Math.floor,Math.ceil,Math.rint,Math.round用法详解

本文详细介绍了Math.floor、Math.ceil、Math.rint 和 Math.round 函数的功能及使用方法,并通过实例展示了这些函数如何处理浮点数。

本文来源于:http://swiftlet.NET/archives/1369

floor原意:地板。Math.floor函数是求一个浮点数的地板,就是求一个最接近它的整数,它的值小于或等于这个浮点数。看下面的例子:

结果为:
Math.floor(-1.1): -2.0
Math.floor(-1.5): -2.0
Math.floor(-1.6): -2.0
Math.floor(0.1): 0.0
Math.floor(0.5): 0.0
Math.floor(0.6): 0.0
Math.floor(1.1): 1.0
Math.floor(1.5): 1.0
Math.floor(1.6): 1.0

Math.ceil函数讲解

ceil愿意:天花板。Math.ceil函数执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。看下面的例子:

结果为:
Math.ceil(-1.1): -1.0
Math.ceil(-1.5): -1.0
Math.ceil(-1.6): -1.0
Math.ceil(0.1): 1.0
Math.ceil(0.5): 1.0
Math.ceil(0.6): 1.0
Math.ceil(1.1): 2.0
Math.ceil(1.5): 2.0
Math.ceil(1.6): 2.0

Math.rint函数讲解

Math.rint函数返回最接近参数的整数,如果有2个数同样接近,则返回偶数的那个。

结果为:
Math.rint(-1.1): -1.0
Math.rint(-1.5): -2.0
Math.rint(-1.6): -2.0
Math.rint(0.1): 0.0
Math.rint(0.5): 0.0
Math.rint(0.6): 1.0
Math.rint(1.1): 1.0
Math.rint(1.5): 2.0
Math.rint(1.6): 2.0

Math.round函数讲解

round方法,它表示"四舍五入",算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整

结果为:
Math.round(-1.1): -1
Math.round(-1.5): -1
Math.round(-1.6): -2
Math.round(0.1): 0
Math.round(0.5): 1
Math.round(0.6): 1
Math.round(1.1): 1
Math.round(1.5): 2
Math.round(1.6): 2

Math.floor()就是下取整,返回一个浮点型值,Math.ceil()是上取整,也返回一个浮点型值,Math.round(),四舍五入,返回一个整型值,Math.rint(), 返回最接近参数的整数,如果有2个数同样接近,则返回偶数的那个,返回一个浮点型值
`math.ceil` 和 `math.floor` 是在多种编程语言中用于处理浮点数取整的函数,以下分别介绍它们在 Python、Java 和 JavaScript 中的用法、区别及示例。 ### Python 在 Python 中,`math.ceil` 和 `math.floor` 函数来自 `math` 模块。 - **用法**: - `math.ceil(x)`:对 `x` 向上取整,返回大于或等于 `x` 的最小整数。 - `math.floor(x)`:对 `x` 向下取整,返回小于或等于 `x` 的最大整数。 - **区别**:`math.ceil` 是向上取整,而 `math.floor` 是向下取整。 - **示例**: ```python import math x = 9.8 print(math.floor(x)) # 输出 9 print(math.ceil(x)) # 输出 10 ``` ### Java 在 Java 中,`Math.ceil` 和 `Math.floor` 是 `Math` 类的静态方法。 - **用法**: - `Math.ceil(double a)`:返回大于或等于参数的最小(最接近负无穷大)整数值,结果为 `double` 类型。 - `Math.floor(double a)`:返回小于或等于参数的最大(最接近正无穷大)整数值,结果为 `double` 类型。 - **区别**:`Math.ceil` 向上取整,`Math.floor` 向下取整。 - **示例**: ```java class Main { public static void main(String[] args) { double testdate = 3.14; System.out.println("Math.floor(" + testdate + ")所对应的值为" + Math.floor(testdate)); System.out.println("Math.ceil(" + testdate + ")所对应的值为" + Math.ceil(testdate)); testdate = -3.14; System.out.println("Math.floor(" + testdate + ")所对应的值为" + Math.floor(testdate)); System.out.println("Math.ceil(" + testdate + ")所对应的值为" + Math.ceil(testdate)); } } ``` ### JavaScript 在 JavaScript 中,`Math.ceil` 和 `Math.floor` 是 `Math` 对象的方法。 - **用法**: - `Math.ceil(x)`:返回大于或等于 `x` 的最小整数。 - `Math.floor(x)`:返回小于或等于 `x` 的最大整数。 - **区别**:`Math.ceil` 向上取整,`Math.floor` 向下取整。 - **示例**: ```javascript console.log(Math.ceil(4.7)); // 输出 5 console.log(Math.floor(4.7)); // 输出 4 console.log(Math.ceil(-4.7)); // 输出 -4 console.log(Math.floor(-4.7)); // 输出 -5 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值