Skip to content Skip to sidebar Skip to footer

How Do I Round To 2 Decimal Places?

I have a number with a comma, for example: 254,5. I need the 0 behind the ,5 so it stands like 254,50 instead.. I'm using this to get the number: Math.floor(iAlt / 50) * 50; How c

Solution 1:

Try the toFixed() method, which pads the decimal value to length n with 0's.

var result = (Math.floor(iAlt / 50) * 50).toFixed(2);

A Number will always remove trailing zeros, so toFixed returns a String.

It's important to note that toFixedmust be called on a number. Call parseFloat() or parseInt() to convert a string to a number first, if required (not in this situation, but for future reference).

Post a Comment for "How Do I Round To 2 Decimal Places?"