如何使用 parseFloat(0.00) 获取浮点数 [英] how to get a float using parseFloat(0.00)
本文介绍了如何使用 parseFloat(0.00) 获取浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获得 0.00 的浮点数.我需要 0.00 的原因是因为我将通过添加来累积浮点值.因此从 0.00 开始.我试过了
How can I get a float of 0.00. The reason I need 0.00, is because I am going to be accumalating float values by adding. Hence starting with 0.00. I tried
var tmp ='0.00'
tmp = parseFloat(tmp.toString()).toFixed(2);
totals = parseFloat(tmp)
tmp 为 0.00,但总数为 0.我怎样才能使总数为 0.00?我需要它保持为浮点数而不是字符串.谢谢
tmp is 0.00 but totals is 0. How can I make total 0.00? I need it to stay as a float and not a string. Thanks
推荐答案
您可以使用字符串 tmp
变量,然后在需要添加时使用:
You can use the string tmp
variable and then when you need to add to it use:
tmp = (+tmp + 8).toFixed(2);
或者只是编写一个函数来执行此操作,因为您必须多次执行此操作:
Or simply write a function to do that seeing that you'll have to do that many times:
function strAdd( tmp, num ) {
return (+tmp + num).toFixed(2);
}
这篇关于如何使用 parseFloat(0.00) 获取浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文