Round a number to two decimal places to format currency

I need to round to two decimal places for the currency.

AND

Math.round(num*Math.pow(10,2))/Math.pow(10,2) 

and

 Math.round(num*Math.pow(10,2))/Math.pow(10,2) 

except that it cuts any trailing zero, so I get 29.9 instead of 29.90 .

What is the best way to do this?

+4
source share
2 answers

You can add this to the number you want to set for a specific format.

 .toFixed(2) 
+13
source
 (Math.round(num*Math.pow(10,2))/Math.pow(10,2)).toFixed(2) 
+3
source

Source: https://habr.com/ru/post/1433955/


All Articles