javascript [Decimal to Percentage retain two decimals]

The decimal similarity encountered in the project (the business gives it a decimal) requires the web interface to be expressed as a percentage, and retain two decimals.

Code directly.

var nval = 0.12345;
var percent = (Math.round(nval*10000))/100+'%';

Description:

1. Math.round (num) function is the operation of rounding num numbers, removing decimal places, leaving only integer bits. For example, 2.11, the returned result is 2; 0.51, the returned result is 1; 0.12345 in this example needs to be multiplied by 10000 to 1235 and then divided by 100, to achieve the purpose of retaining two decimal places.

2. It should be noted that except 100 is placed outside math.round (). I think a lot of posts are put in it, then it is impossible to keep the result of two decimal places. . . A little irresponsible.

Published 25 original articles · Like 28 · Visit 110,000+

Guess you like

Origin blog.csdn.net/zcc1229936385/article/details/105403853