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
1262 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
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