Javascript round to given precision

linkaiyi
Follow

Javascript round to given precision

Use Math.round(num * 100) / 100

Edit: to ensure things like 1.005 round correctly, we use

Math.round((num + Number.EPSILON) * 100) / 100

or use string function toFixed

If the value is a text type:

parseFloat("123.456").toFixed(2);

If the value is a number:

var numb = 123.23454;
numb = numb.toFixed(2);
Object has 0 attachments

Was this article helpful?

This article is viewed 14 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support