How to round the number up to 2 digits in Javascript

Sometimes you need to round up the number up to 2 digits. For example, you calculation has to show a result in currency.

There are two solutions to use on internet:

1) Use Math.random function.

var a=123.4567;

Math.random(a*100)/100

However, this is not really good, as for example in case of
var a=1.005;
Math.random(a*100)/100 results into 1 instead of 1.01.

2) use toFixed(2)

However, it is more secure option to use a.toFixed(2), which would return element as fixed 2 decimal value

Comments

Popular posts from this blog

Exporting Skype Chat/Skype Contacts to csv file using the shell script and sqlite3 (usually already installed on mac)

trim() not supported by IE7 and IE8

Finally, clear explanation of TinyOS 2.x serial packet structure