Home > Web Front-end > JS Tutorial > body text

Explanation on how to obtain the number of digits after the decimal point using JavaScript

巴扎黑
Release: 2017-08-14 13:48:45
Original
1270 people have browsed it

这篇文章主要介绍了javascript 取小数点后几位几种方法总结的相关资料,这里提供了四种方法,帮助大家整理,需要的朋友可以参考下

javascript 取小数点后几位方法总结

Javascript取float型小数点后两位,例22.123456取成22.12,如何做?

1.通过substring截取。


function getnum()
{
var num = 22.123456;
var result = num.substring(0,s.indexOf(".")+3);
alert(result);
}
Copy after login

2. 正则表达式。


function getnum()
{
var num = 22.123456;
var aNew;
var re = /([0-9]+\.[0-9]{2})[0-9]*/;
aNew = num.replace(re,"$1");
alert(aNew);
}
Copy after login

3.数据类型保留上。


function getnum()
{
var num=22.123456;
alert( Math.round(num*100)/100);
}
Copy after login

4.toFixed方法


alert(num.toFixed(2));
Copy after login

The above is the detailed content of Explanation on how to obtain the number of digits after the decimal point using 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!