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

高洛峰
高洛峰Original
2016-12-09 11:52:291273browse

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));


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