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
精彩评论