开发者

How can I move the decimal point to the 100ths of an integer?

开发者 https://www.devze.com 2023-01-30 08:51 出处:网络
This seems like a silly question but I cant figure out how to convert an integer number that represent cents to dollars.

This seems like a silly question but I cant figure out how to convert an integer number that represent cents to dollars.

3000 -> 30.00

in javascript...

I was using ParseFloat but it's only giving me back the integer =/ I need to开发者_JAVA技巧 always display the cents even if its 0.


Use toFixed().

var num = 3000;

alert( (num/100).toFixed( 2 ) ); // alerts 30.00


Try something similar to:

document.write(x.toFixed(2));


You can divide by one hundred, and then call the toFixed method to format it to two decimal places.

myNumber.toFixed(2)

Edit: "Then" was "ten" :\


I combined the toFixed(2) method others have provided with a pre-existing currency formatter if anyone stumbling upon this finds it handy.

cents-to-currency

Not trying to do any self-promo or anything :D

0

精彩评论

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