十进制数会消失

时间:2014-02-06 11:05:13

标签: javascript

我的代码:

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 0.30' )

我的输出为 10.3

如果我使用.tofixed(2)

  

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

我的输出 10.30 ,但它在 STRING ,这对我来说是个大问题,因为我希望输出为数字。< / p>

如果我喜欢这个

  

Number(parseFloat('10 .30')。toFixed(2))

我得到了输出 10.3

但我希望输出的数量和小数点 像这样10.30

Plz帮助...... !!!

3 个答案:

答案 0 :(得分:0)

试试这个:

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

See Live Demo

答案 1 :(得分:0)

我不知道你在做什么,但我也做了同样的事情,得到了理想的结果。

我的代码:

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

见这里:Fiddle

答案 2 :(得分:0)

感谢 Passerby 寻求帮助。

最后我从中得到了结果:

  <$> $ price.html(($ price.data(“price”)+ parseFloat($ label.find(“。badge span”)。text()|| 0,10))。toFixed(2));