Possible Duplicate:
Truncate Decimal number not Round Off in jquery
I have values like 5.3777777
, 4.6666666
on my Y
variable.
How can I truncate it so it shows 5.3 or 5.6 instead with JavaScript?
Thanks in advance!
Possible Duplicate:
Truncate Decimal number not Round Off in jquery
I have values like 5.3777777
, 4.6666666
on my Y
variable.
How can I truncate it so it shows 5.3 or 5.6 instead with JavaScript?
Thanks in advance!
The standard way to do this in any language is to multiple the value by the inverse of the precision required, truncate, and then divide again:
var truncated = Math.floor(val * 10) / 10;
If you want strict rounding rather than truncation then Javascript also has Number.toFixed()
built-in which does this for you.