admin管理员组文章数量:1221321
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 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
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 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
Share Improve this question asked Jun 24, 2014 at 19:12 MaryMary 1,5956 gold badges30 silver badges47 bronze badges 2 |5 Answers
Reset to default 5You can use the string tmp
variable and then when you need to add to it use:
tmp = (+tmp + 8).toFixed(2);
JSFIDDLE DEMO
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);
}
There is no such thing as an integer type in javascript. There is only Number
, which is stored as a double precision floating point. So to get a floating point value with 0.00, you need only to do this:
var tmp = 0;
var tmp ='0.00'
tmp = parseFloat(tmp.toString()).toFixed(2);
totals=parseFloat(tmp).toFixed(2);
alert(totals); //0.00
parseFloat()
without toFixed()
removes zeros after dot. So you need to add toFixed()
again.
Here is dynamic floatParser for those who need
function customParseFloat(number){
if(isNaN(parseFloat(number)) === false){
let toFixedLength = 0;
let str = String(number);
// You may add/remove seperator according to your needs
[".", ","].forEach(seperator=>{
let arr = str.split(seperator);
if( arr.length === 2 ){
toFixedLength = arr[1].length;
}
})
return parseFloat(str).toFixed(toFixedLength);
}
return number; // Not a number, so you may throw exception or return number itself
}
You can use parseFloat function in Javascript.
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var a = parseFloat("10") + "<br>";
var b = parseFloat("10.00") + "<br>";
var c = parseFloat("10.33") + "<br>";
var d = parseFloat("34 45 66") + "<br>";
var e = parseFloat(" 60 ") + "<br>";
var f = parseFloat("40 years") + "<br>";
var g = parseFloat("He was 40") + "<br>";
var n = a + b + c + d + e + f + g;
document.getElementById("demo").innerHTML = n;
}
</script>
本文标签: javascripthow to get a float using parseFloat(000)Stack Overflow
版权声明:本文标题:javascript - how to get a float using parseFloat(0.00) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739358778a2159720.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Number
. It just happens to strip off the.00
when you're displaying it. See Be careful with JS numbers! – p.s.w.g Commented Jun 24, 2014 at 19:17.00
is purely a matter of formatting. – C3roe Commented Jun 24, 2014 at 19:18