开发者

Javascript Decimals

开发者 https://www.devze.com 2023-04-04 09:49 出处:网络
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:

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号