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 always display the cents even if its 0.
To round a number to the nearest hundredth, look at the digit at the right of the hundredth place and apply the same rule : if the digit in the thousandth place is 0, 1, 2, 3, 4, you will round down to the nearest hundredth or just remove all digits found at the right.
The first digit after the decimal represents the tenths place. The next digit after the decimal represents the hundredths place.
The difference is that the decimal point moves to the right when you multiply; it moves to the left when you divide.
Use toFixed()
.
var num = 3000;
alert( (num/100).toFixed( 2 ) ); // alerts 30.00
Try something similar to:
document.write(x.toFixed(2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With