Home > Web Front-end > JS Tutorial > A simple way to get two or n digits after the decimal point of a number in JS

A simple way to get two or n digits after the decimal point of a number in JS

高洛峰
Release: 2016-12-09 11:52:29
Original
1318 people have browsed it

In js, sometimes it is necessary to get the two decimal places of a float number.

var num=10/3;
function f(num,n){
    return parseInt(num*Math.pow(10,n)+0.5,10)/Math.pow(10,n);
  }
alert(f(num,2));
Copy after login


Related labels:
js
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template