JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解
首先还是看看JavaScript: The Definitive Guide, 4th Edition中对三个函数的定义。
Math.ceil(): round a number up
Arguments: Any numeric value or expression
Returns: The closest integer greater than or equal to x.
-----------------------------------------------------------------------------------------------
Math.floor(): round a number down
Arguments: Any numeric value or expression
Returns: The closest integer less than or equal to x.
-----------------------------------------------------------------------------------------------
Math.round(): round to the nearest integer
Arguments: Any number.
Returns: The integer closest to x.
以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数。
现在做一个总结:
1. Math.ceil()用作向上取整。
2. Math.floor()用作向下取整。
3. Math.round() 我们数学中常用到的四舍五入取整。
相关文章
JavaScript中Number.MAX_VALUE属性的使用方法
这篇文章主要介绍了JavaScript中Number.MAX_VALUE属性的使用方法,是JS入门学习中的基础知识,需要的朋友可以参考下2015-06-06javascript中的遍历for in 以及with的用法
这篇文章主要介绍了javascript中的类初始化,遍历for in 以及with的用法,需要的朋友可以参考下2014-12-12Javascript排序算法之合并排序(归并排序)的2个例子
这篇文章主要介绍了Javascript排序算法之合并排序(归并排序)的2个例子,需要的朋友可以参考下2014-04-04深入解读JavaScript中的Iterator和for-of循环
这篇文章主要介绍了JavaScript中的Iterator和for-of循环,是JS入门学习中的基础知识,需要的朋友可以参考下2015-07-07
最新评论