Javascript 十进制数消失了
我的代码:Javascript 十进制数消失了,javascript,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 } $pr
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**
*/
var str = '10.30';
var flo = parseFloat(str);
alert(flo);
flo = flo.toFixed(2)
alert(flo);
我有一根绳子
“10.30”
现在,如果我使用parsefloat将字符串转换为数字
parseFloat('10.30')
我得到的输出为10.3
如果我使用.tofixed(2)
parseFloat('10.30')。toFixed(2)
我得到了输出10.30,但它在字符串中,这对我来说是个大问题,因为我希望输出为数字
如果我真的喜欢这个
数字(浮动('10.30')。固定(2))
我得到了输出10.3
但是我想要数字和小数点的输出
像这样10点30分
请帮忙 试试这个:
parseFloat(Math.round(num3 * 100) / 100).toFixed(2);
我不知道你在做什么,但我也做了同样的事情,并得到了预期的结果 我的代码:
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**
*/
var str = '10.30';
var flo = parseFloat(str);
alert(flo);
flo = flo.toFixed(2)
alert(flo);
请参见此处:感谢路人的帮助 最后,我得到了以下结果: $price.html($price.data(“price”)+parseFloat($label.find(.badge span”).text()| | 0,10)).toFixed(2))
数字
10.3
与10.30
完全相同,那么为什么它在数字上很重要呢?如果是为了在网页中显示,那么字符串就可以了。最后一个数字是0,它与包含它的浮点计算无关。当你输出一些东西时,它会变成一个字符串,那么到底是什么问题呢?请查看@DavidThomas::>Ya,我知道最后一个数字是零。但是,对我来说,这非常重要。对于电子商务网站和产品价格,我必须显示小数。@ParaMeterz或者如果您只关心.html()
调用:$price.html(($price.data(“price”)+parseFloat($label.find(“.badge span”).text()| 0,10)).toFixed(2))代码>谢谢你的帮助。但是我已经检查了这个演示和你的代码,但是输出是字符串的(但我想要数字。当我们使用.tofixed()时,情况是这样的。它会将数字转换为字符串。:(没有人,它没有修复。我希望最终输出为数字而不是字符串。如果使用.tofixed(),它会将输出转换为字符串。签出最后一个警报人typeof(flo)是字符串,但我想要number.alert(typeof(flo));是的,我看到了……实际上toFixed()
总是将浮点数转换为字符串…检查这里:如果你想要一个数字作为10.30
,那么不管它是10.30
还是10.3
,如果你想在某处显示它,那么字符串
就可以了。你到底在哪里使用这个呢???是的,我知道最后一个数字是零,我知道esn没关系。但是,在我的例子中,这非常重要。对于产品价格,我必须显示小数。在这种情况下,我必须添加两个字段“价格+折扣”,这两个字段都是字符串。因此,我必须首先将它们转换为数字,然后才能添加。这就是为什么我希望输出的数字带有小数点0的原因:(