Home >Web Front-end >JS Tutorial >How to convert decimal to percentage in javascript
How to convert decimals to percentages in javascript: 1. Use the "decimal value * 100" statement to multiply the decimal by 100 and obtain the percentage part of the decimal converted into a percentage; 2. Use the "percent value" The '%'" statement adds a percent sign after the percentage value to concatenate it into a percentage.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript converts decimals into percentages
Percents have two parts: the percent value (decimal multiplied by 100) and the percent sign" %".
Implementation idea:
First multiply the decimal by 100
Then use " " to splice the percent sign
Implementation code:
var nval = 0.12345; var percent = (nval*100)+'%'; console.log(percent);
If you want to convert a decimal to a percentage and retain two decimal places, you can do this:
var nval = 0.12345; var percent = (Math.round(nval*10000))/100+'%'; console.log(percent);
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to convert decimal to percentage in javascript. For more information, please follow other related articles on the PHP Chinese website!