您好,欢迎访问一九零五行业门户网

JavaScript如何实现数据的四舍五入

实现数据的四舍五入有以下几种方法:round方法、tofixed方法、parseint方法、ceil方法以及floor方法
在javascript 对数值进行四舍五入的操作有以下几种
round()方法:可把一个数字舍入为最接近的整数,即四舍五入
tofixed()方法:可把 number 四舍五入为指定小数位数的数字。
parseint()方法:可将小数转化为整数(位操作符)
ceil()方法:可对一个数进行上舍入
floor()方法:可对一个数进行下舍入
接下来在文章中将和大家详细介绍这几种方法的具体用法
round()方法
document.write(math.round(4.55) + "<br>"); document.write(math.round(-6.45));
效果图:
tofixed()方法
var num=3.141592;document.write(num.tofixed(2));
效果图:
parseint()方法
document.write(parseint("12.333"));
效果图:
ceil()方法与floor()方法
document.write("ceil方法:")document.write(math.ceil("12.333") + "<br>");document.write("floor方法:")document.write(math.floor("12.333"));
效果图:
以上就是javascript如何实现数据的四舍五入的详细内容。
其它类似信息

推荐信息