Home > Web Front-end > Front-end Q&A > How to convert percentage to decimal in javascript

How to convert percentage to decimal in javascript

WBOY
Release: 2022-02-08 14:30:46
Original
5505 people have browsed it

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)
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template