Home  >  Article  >  Web Front-end  >  How to convert percentage to decimal in javascript

How to convert percentage to decimal in javascript

WBOY
WBOYOriginal
2022-02-08 14:30:465451browse

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.

How to convert percentage to decimal in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to convert a percentage to a decimal in javascript

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:

How to convert percentage to decimal in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn