2014-02-06 39 views
0

我的代码:十进制数越来越即消失

var $label = $(ev.currentTarget); 
var $price = $label.parent("form").find(".oe_price .oe_currency_value"); 

if (!$price.data("price")) { 
    $price.data("price", parseFloat($price.text()); //price is 10.30 but it returns 10.3 as number 
} 

$price.html($price.data("price")+parseFloat($label.find(".badge span").text() || 0)); 
/* The value coming in **badge** is **12.00** 
* but parseFloat converts that into **12** 
* thats why after the addition I got the output as **22.3** 
* but i want it as **22.30** 
*/ 

我有一个字符串

'10 0.30'

现在,如果我转换使用parsefloat

字符串到数字

parseFloat('10.30')

我得到的输出10.3

,如果我是用.tofixed(2)

parseFloat('10 0.30' )做的。toFixed(2)

我得到了输出10.30但它在STRING这对我来说是个大问题,因为我想输出为号码

如果我不喜欢这样

数(parseFloat('10 0.30' )。toFixed(2))

我得到的输出10.3

但我想在输出数和小数点 这样10.30

PLZ帮助... !!!

+1

数字'10.3'与'10.30'完全相同,所以为什么它在数字上很重要?如果是在网页中呈现,那么字符串就可以了。 –

+0

最后一位数字是0,与浮点计算无关,因此将其包含在内。当你输出某些东西时,它会变成一个字符串,那么究竟是什么问题呢? – gpgekko

+0

请检查http://stackoverflow.com/questions/2221167/javascript-formatting-a-rounded-number-to-n-decimals/2909252#2909252 –

回答

0

thankz 路人的帮助。

最后我得到这个结果:

$ price.html(($ price.data( “价格”)+ parseFloat($ label.find( “徽章跨度”)文本( )|| 0,10))。toFixed(2));

0

试试这个:

parseFloat(Math.round(num3 * 100)/100).toFixed(2); 

See Live Demo

+0

Thankz寻求帮助。但我已经检查过演示和代码你的,但输出的是字符串。:( 但我想要数量。 情况是这样的,当我们使用.tofixed()它会将数字转换为字符串。 :( – ParaMeterz

+0

它解决了你的问题吗?编辑:好的! – Jzf

+0

没有人,它没有修复。 我想最终输出为数字不是字符串。 如果使用.toFixed()它会将输出转换为字符串。 – ParaMeterz

0

我不知道你在做什么,但我已经做了同样的和得到期望的结果。

我的代码:

var str = '10.30'; 
var flo = parseFloat(str); 
alert(flo); 
flo = flo.toFixed(2) 
alert(flo); 

在这里看到:Fiddle

+0

检查最后一个警报人的typeof(flo)是字符串,但我想要的数字。 alert(typeof(flo)); – ParaMeterz

+0

是的,我看到了这个...实际上'toFixed()'总是将float转换为字符串...在这里检查:http://stackoverflow.com/q/2283566/2764279 –

+0

如果你想要一个'数字'作为'10.30',比不管它是否是'10.30'或'10.3',并且如果你想把它显示在某处然后用'String'就可以了。 你究竟在哪里使用这个??? –