Reputation: 853
How to keep '.00' in Javascript? I currently have the string "123456789012.00"
;
I want to get the double 123456789012.00
to keep .00
toFixed(2)
will return a stringparseFloat()
will cast the .00
How can I do this?
Upvotes: 1
Views: 2308
Reputation: 7990
You can use the toFixed()
method to do this. The code below will log the result you want in the console of your browser.
var num = "123456789012.00";
console.log(parseFloat(num).toFixed(2));
Upvotes: 1
Reputation: 33139
A float uses the precision it needs (that's why it's called a "float" -- as in "floating point", the point has no fixed position).
If you want to display a float with the 2 significant digits (i.e. 2 digits after the point), you can use toFixed(2)
. That will not change the number, but will store it in a string with the number of digits you want to show.
Upvotes: 8