Javascript Decimals
How can I convert a integer type to a double/float type so it shows decimal points? For instance if I want to convert a number to a money format:
5 would turn into 5.00 4.3 would turn into 4.30
Doe开发者_运维技巧s javascript have something I can use to do this kind of conversion?
You want toFixed:
var a = 5;
a.toFixed(2); // '5.00'
4.3.toFixed(2); // '4.30'
You can use toFixed(n)
, where n
is the number of decimal places.
5 .toFixed(2);
//-> "5.00"
4.3 .toFixed(2);
//-> "4.30"
精彩评论