码迷,mamicode.com
首页 > 编程语言 > 详细

javaScript中小数取整,四种方法的比较

时间:2016-08-15 12:49:03      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:


1.parseInt:只取整数位
例如:
parseInt(3.7) 取整结果为:3
parseInt(-1.1) 取整结果为:-1

2.Math.floor :向下去整,取大的整数
例如:
Math.floor(3.7) 取整结果为:4
Math.floor(-1.1) 取整结果为:-1


3.Math.ceil :向上去整,取小的整数
例如:
Math.floor(3.7) 取整结果为:3
Math.floor(-1.1) 取整结果为:-2

4.Math.round:四舍五入
例如:
Math.round(3.3) 取整结果为:3
Math.round(3.5) 取整结果为:4
Math.round(3.7) 取整结果为:4
Math.round(-1.3) 取整结果为:-1
Math.round(-1.5) 取整结果为:-1
Math.round(-1.6) 取整结果为:-2

 

javaScript中小数取整,四种方法的比较

标签:

原文地址:http://www.cnblogs.com/caozong/p/5772453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!