`
Dream丶小雅
  • 浏览: 47289 次
文章分类
社区版块
存档分类
最新评论

用floor(),ceil(),round()教你如何向上,向下,四舍五入取整

阅读更多

Math对象用于执行数学任务。

floor()方法可对一个数向下取舍。

语法

Math.floor(x)

 floor()方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。

例如:

<script type="text/javascript">

document.write(Math.floor(0.60) + "<br />")
document.write(Math.floor(0.40) + "<br />")
document.write(Math.floor(5) + "<br />")
document.write(Math.floor(5.1) + "<br />")
document.write(Math.floor(-5.1) + "<br />")
document.write(Math.floor(-5.9))

</script>

 输出:

0
0
5
5
-6
-6

 round()方法可把一个数字舍入为最接近的整数。

Math.round(x)

 round()方法执行的是四舍五入

例如:

<script type="text/javascript">

document.write(Math.round(0.60) + "<br />")
document.write(Math.round(0.50) + "<br />")
document.write(Math.round(0.49) + "<br />")
document.write(Math.round(-4.40) + "<br />")
document.write(Math.round(-4.60))

</script>

 输出:

1
1
0
-4
-5

 ceil()方法可对一个数进行上舍入。

Math.ceil(x)

  ceil()方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。

例如:

<script type="text/javascript">

document.write(Math.ceil(0.60) + "<br />")
document.write(Math.ceil(0.40) + "<br />")
document.write(Math.ceil(5) + "<br />")
document.write(Math.ceil(5.1) + "<br />")
document.write(Math.ceil(-5.1) + "<br />")
document.write(Math.ceil(-5.9))

</script>

 输出:

1
1
5
6
-5
-5

 

分享到:
评论

相关推荐

    Oracle SQL语句实现数字四舍五入取整

    取整(向下取整): 代码如下:select floor(5.534) from dual...四舍五入: 代码如下:SELECT round(5.534) FROM dual; SELECT round(5.534,0) FROM dual; SELECT round(5.534,1) FROM dual; SELECT round(5.534,2) FR

    DB2数值函数简介及使用

    6、四舍五入:round (m,n) 7、将数值转换为字符串形式:digits()函数 8、转换为数值:to_number(string,format) 9、数值截取函数:trunc(m,n)或者truncate(m,n) 10、转换为浮点数:decfloat(m,16|34) 11、数值比较...

    js中小数向上取整数,向下取整数,四舍五入取整数的实现(必看篇)

    js中小数向上取整数,向下取整数,四舍五入取整数的实现。 1.ceil() 向上————-ceil英文意思是:天花板———–就是在上 2.floor() 向下————-ceil英文意思是:地面———–就是在下 3.round() 四舍五入——...

    免费开源!! Java 面试必会 直通BAT

    # 1.35 Math.round(11.5)等於多少?...round 方法,它表示“四舍五入”,算法为 Math.floor(x+0.5),即将原来的数字加上0.5后再向 下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。

    jquery向上向下取整适合分页查询

    在用ajax进行分页查询时,分页的数目要用到取整函数 [removed] var uu=Math.floor(5.36) //向下取整 结果为5 var uu=Math.floor(5.88) /...Math.round(5.55) //四舍五入 结果为6 math.round(5.22) //结果为5 [removed]

    JavaScript十大取整方法实例教程

    1. parseInt() // js内置函数,注意接受参数是string,所以调用该方法时存在类型转换 parseInt(1.5555) // =&gt; 1 2. Number.toFixed(0) // 注意toFixed返回的字符串,若想获得整数还需要做类型...// 四舍五入取整 Ma

    Javascript Math ceil()、floor()、round()三个函数的区别

    Round是四舍五入的。。。Ceiling是向上取整。。float是向下取整

    oracle 数字函数

    取整函数(ceil 向上取整 floor 向下取整) 取幂 power 和 求平方根 sqrt 求余 返回固定小数位数 round:四舍五入 trunc:直接截断 返回值的符号 正数返回为1 负数为 1

    总结PHP中数值计算的注意事项

    2:floor — 舍去法取整(向下取整) float floor ( float $value ) 3.ceil — 进一法取整(向上取整) float ceil ( float $value ) 坑点: 当数值为整数的时候 例如 11 那么floor(11) = 10 , ceil (11) = 12;...

    详解JavaScript中|单竖杠运算符的使用方法|javascript-214288.pdf

    3. Math.round() 我们数学中常用到的四舍五入取整。 console.log(0.6|0)//0 console.log(1.1|0)//1 console.log(3.65555|0)//3 console.log(5.99999|0)//5 console.log(-7.777|0)//-7 注:除了Math的

    mysql入门.md

    floor向下取整 ceil向上取整 mod取余 truncate截断 ##### 3、日期函数 now当前系统日期+时间 curdate当前系统日期 curtime当前系统时间 str_to_date 将字符转换成日期 date_format将日期转换成字符 ##### 4...

    一看就懂得Python的math模块

    r = round(3.4999) # 四舍五入 print(r) r = math.pow(3,4) # =3**4 print(r) r = math.sqrt(25) # 开平方 print(r) r = math.fabs(-0.23) # 绝对值(小数) print(r) r = abs(-2) # 绝对值 print(r) r = math.modf...

    马哥python课堂笔记-马哥教育PYTHON相关基础笔记.pdf

    6 数字的处理函数 math.ceil #向上取整(天花板) math.floor (i2nt)#向下取整, round() # 四舍六⼊ 五取偶 (2.5=2,3.5=4) 7 元组 y = (1,)逗号代表元组 命名元组namedtuple() from collections import namedtuple ...

    js 小数取整的函数

    1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2)

    JavaScript的数学对象

    Math对象 Math 是JS的内置对象,用于执行数学任务。 一、属性(常量) 1. Math.PI 圆周率 返回圆周率 π 2.Math.LN2 2 的自然对数 ...3.Math.floor(x) 向下取整 返回值:如果 x 是一个整数,返回 x 如果 x 是一个小

    js生成随机数的过程解析

    //四舍五入。 Math.random(); //0.0 ~ 1.0 之间的一个伪随机数。【包含0不包含1】 //比如0.8647578968666494 Math.ceil(Math.random()*10); // 获取从1到10的随机整数 ,取0的概率极小。 Math.round(Math.random())...

    JavaScript Math 对象常用方法总结

    Math.round(x):四舍五入 获取指定范围内的随机数 var x=Math.floor(Math.random()*(max-min+1))+min; 以上这篇JavaScript Math 对象常用方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家...

    详解数据库_MySQL: mysql函数

    四舍五入 向上取整 向下取整 2.字符串函数 length() 字节长度 char_length() 字符长度 ucase() 大写 lcase() 小写 concat(字符,…,字符n) 连接字符串 replace(字符串,旧字符,新字符)字符串替换 截取字符串 ...

    js取整数、取余数的方法

    3,四舍五入. Math.round(5/2) 4,向下取整  Math.floor(5/2) Math 对象的方法FF: Firefox, N: Netscape, IE: Internet Explorer 方法 描述 FF N IEabs(x) 返回数的绝对值 1 2 3acos(x) 返回数的反余弦值 1 2 3asin...

    matlab源码求一元函数-matlabstudyrporter:matlabstudyrporter

    四舍五入 ceil 向上取整 floor 向下取整 fix 向零取整 rem取余数 isprime判断素数 find找对应序号 who命令和whos命令 save和load命令 save mydata a x 11.inv矩阵求逆 11.str2double() %str to double num2cell()把...

Global site tag (gtag.js) - Google Analytics