数学对象Math ceil()、floor()、round()方法
|
Math.ceil()
|
|
功能:对一个数进行上取整。
语法:Math.ceil(x)
参数:
返回值:返回大于或等于x,并且与之最接近的整数。
注:如果x是正数,则把小数“入”;如果x是负数,则把小数“舍”。
例:
<script type="text/javascript">
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) ); </script> 输出结果为:
document.write( Math.ceil(1.2)+", "+Math.ceil(1.8)+", "+Math.ceil(-1.2)+", "+Math.ceil(-1.8) ); 2, 2, -1, -1
|
|
Math.floor()
|
|
功能:对一个数进行下取整。
语法:Math.floor(x)
参数:
返回值:返回小于或等于x,并且与之最接近的整数。
注:如果x是正数,则把小数“舍”;如果x是负数,则把小数“入”。
例:
<script type="text/javascript">
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) ); </script> 输出结果为:
document.write( Math.floor(1.2)+", "+Math.floor(1.8)+", "+Math.floor(-1.2)+", "+Math.floor(-1.8) ); 1, 1, -2, -2
|
|
Math.round()
|
|
功能:四舍五入取整。
语法:Math.round(x)
参数:
返回值:与x最接近的整数。
例:
<script type="text/javascript">
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) ); </script> 输出结果为:
document.write( Math.round(1.2)+", "+Math.round(1.8)+", "+Math.round(-1.2)+", "+Math.round(-1.8) ); 1, 2, -1, -2
|
原文链接:http://blog.csdn.net/xiaxiaorui2003/article/details/6481034
数学对象Math ceil()、floor()、round()方法的更多相关文章
- javascript中Math ceil(),floor(),round()三个函数的对比
Math.ceil()执行的是向上舍入 Math.floor()执行向下舍入 Math.round()执行标准舍入 一下是一些补充: ceil():将小数部分一律向整数部分进位. 如: Math.ce ...
- C语言中几个常用数学计算函数ceil(), floor(), round()的用法
最近在实现算法的过程中,遇到了使用几个数学计算函数,感觉挺有意思,就记下来 方便以后使用. ceil(x)返回不小于x的最小整数值(然后转换为double型). floor(x)返回不大于x的最大整数 ...
- Math类的round方法小解
在Math类中有三个关于“四舍五入”的静态方法(ceil,floor,round): ① Math.ceil(number) 向上取整,Math.ceil(11.2) 结果:12 ...
- JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结
floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下 ...
- Math对象(min()-max()-ceil()-floor()-round()和abs())
1.Math.min() 功能:求一组数的最小值,返回值:Number 如果里面有非数字则返回NaN 2.Math.max() 功能:求一组数的最大值,返回值:Number 3.Math.ceil() ...
- Math中floor,round和ceil的区别
floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数(当-1.5时可见,四舍五入后得到的结果不是我们期待的,解决办法是先对他取绝对值,然后在用round方法) r ...
- Java Math的 floor,round和ceil的总结
floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下 ...
- Java Math的 floor,round和ceil
floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数 round方法,它表示"四舍五入",算法为Math.floor(x+0.5),即将原来的 ...
- java floor,ceil和round方法
Math.floor():返回值是double类型的,返回的是不大于它的最大整数 举例: double x = Math.floor(5.8); System.out.println(x); //输出 ...
随机推荐
- PLSQL_性能优化系列20_Oracle Result Cash结果缓存
20150528 Created By BaoXinjian
- 实现读入一个彩色视频文件并以灰度格式输出这个视频文件,学习opencv例2-10
#include "cv.h"#include "highgui.h"int main(int argc,char* argv[]){ //书本中的main没有 ...
- Python 向上取整的算法
一.初衷: 有时候我们分页展示数据的时候,需要计算页数.一般都是向上取整,例如counts=205 pageCouts=20 ,pages= 11 页. 一般的除法只是取整数部分,达不到要求. 二.方 ...
- openGL学习:Visual Studio配置OpenGL
Visual Studio配置OpenGL 分两步: (一)下载glut,并将其放置到适当的位置 (二) 进行vs2005项目属性配置 ******************************** ...
- html table单双行颜色间隔(转载)
直接上代码: <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- MemcacheQ 的安装与使用
1.安装libevent 官网:http://www.libevent.org/ 全选复制放进笔记 $ wget https://github.com/downloads/libevent/libev ...
- Centos下yum配置lnmp环境
首先关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=tar ...
- MySql 存储过程实例(附完整注释)
将下面的语句复制粘贴可以一次性执行完,我已经测试过,没有问题! MySql存储过程简单实例: ...
- bootstrap-下拉菜单
<!-- 1.给父级添加class为dropdown,或者给父级添加定位属性 2.给button添加data-toggle="dropdown"属性,并添加dropdown- ...
- 慎用StringEscapeUtils.escapeHtml方法【转】
推荐使用Apache commons-lang的StringUtils来增强Java字符串处理功能,也一直在项目中大量使用StringUtils和StringEscapeUtils这两个实用类. 最近 ...