18

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.

qodeninja
  • 10,946
  • 30
  • 98
  • 152

4 Answers4

52

Use toFixed().

var num = 3000;

alert( (num/100).toFixed( 2 ) ); // alerts 30.00
inanutshellus
  • 9,683
  • 9
  • 53
  • 71
user113716
  • 318,772
  • 63
  • 451
  • 440
1

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" :\

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
1

Try something similar to:

document.write(x.toFixed(2));
Drew D.
  • 411
  • 3
  • 9
1

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

corysimmons
  • 7,296
  • 4
  • 57
  • 65