Method: 1. Use the replace() method to remove the percent sign from the percentage. The syntax is "Percent object.replace("%","")"; 2. Use the "/" operator to remove the To divide the number with the percent sign by 100, the syntax is "percentage minus the percent sign/100"; 3. Output the percentage that has been divided by 100.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
The replace() method is used to replace some characters with other characters in a string, or to replace a substring that matches a regular expression.
Syntax
stringObject.replace(regexp/substr,replacement)
Return value
A new string obtained by replacing the first match or all matches of regexp with replacement.
Convert percentage to decimal
1. Remove the percent sign first
2. Then divide by 100
3. Return out
Examples are as follows:
<html> <body> <script type="text/javascript"> var percent = "4.2%";//申明要放在函数前 function toPoint(percent){ var str=percent.replace("%",""); str= str/100; return str; } toPoint(percent); var result = toPoint(percent); document.write(result);//0.042 </script> </body> </html>
Output results:
Related recommendations: javascript learning tutorial
The above is the detailed content of How to convert percentage to decimal in javascript. For more information, please follow other related articles on the PHP Chinese website!