Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解

来源:互联网 发布:剑桥美国经济史知乎 编辑:程序博客网 时间:2024/05/09 15:06

首先还是看看JavaScript: The Definitive Guide, 4th Edition中对三个函数的定义。

Math.ceil(): round anumber up

Arguments: Any numeric value or expression

Returns: The closest integer greater than or equal tox.

-----------------------------------------------------------------------------------------------

Math.floor(): round a number down

Arguments: Any numeric value or expression

Returns: The closest integer less than or equal tox.

-----------------------------------------------------------------------------------------------

Math.round(): round to thenearest integer

Arguments: Any number.

Returns: The integer closest tox.

 

以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数。

现在做一个总结:

1. Math.ceil()用作向上取整。

2. Math.floor()用作向下取整。

3. Math.round() 我们数学中常用到的四舍五入取整。




转自:http://www.jb51.net/article/23276.htm

0 0
原创粉丝点击