2021-06-18Math.floor,Math.round,Math.ceil的区别

    在java语言和javascript语言中都有Math.floor,Math.round,Math.ceil等方法,他们是有一些区别的,都是在对浮点数做处理,他们的功能,从字面意思就可以看出来,floor就是地板,向下取整,round做动词有四舍五入的意思,因此就是做四舍五入的,ceil动词是装天花板,是向上取整。

     这三者还有一个很明显的区别:floor,ceil都是返回double类型,而round返回的是long类型。

    下面给出一个示例,我们从示例中感受一下他们用法的区别:

package com.xxx.huali.hualitest.mathmethod;
public class MathDemo {
	//+
	private static final double a = 9.4;
	private static final double b = 9.5;
	private static final double c = 9.6;
	//-
	private static final double e = -9.4;
	private static final double f = -9.5;
	private static final double g = -9.6;
	
	
	//floor
	//n.地板;地面;(车厢内的)底板;楼层
	//向下取整
	public static void floor() {
		System.out.println("floor(9.4)="+Math.floor(a));
		System.out.println("floor(9.5)="+Math.floor(b));
		System.out.println("floor(9.6)="+Math.floor(c));
		System.out.println("floor(-9.4)="+Math.floor(e));
		System.out.println("floor(-9.5)="+Math.floor(f));
		System.out.println("floor(-9.6)="+Math.floor(g));
	}
	//round
	//v. 绕行;绕过;(使)成圆形;变圆;(将数字调高或调低)使成为整数;把(数字)四舍五入;
	//四舍五入
	public static void round() {
		System.out.println("round(9.4)="+Math.round(a));
		System.out.println("round(9.5)="+Math.round(b));
		System.out.println("round(9.6)="+Math.round(c));
		System.out.println("round(-9.4)="+Math.round(e));
		System.out.println("round(-9.5)="+Math.round(f));
		System.out.println("round(-9.6)="+Math.round(g));
	}
	//ceil
	//v.装天花板;装壁板;装隔板
	//向上取整
	public static void ceil() {
		System.out.println("ceil(9.4)="+Math.ceil(a));
		System.out.println("ceil(9.5)="+Math.ceil(b));
		System.out.println("ceil(9.6)="+Math.ceil(c));
		System.out.println("ceil(-9.4)="+Math.ceil(e));
		System.out.println("ceil(-9.5)="+Math.ceil(f));
		System.out.println("ceil(-9.6)="+Math.ceil(g));
	}
	
	public static void main(String[] args) {
		System.out.println("向下取整");
		floor();
		System.out.println("四舍五入");
		round();
		System.out.println("向上取整");
		ceil();
	}
}

    运行代码,打印信息如下:

    从打印结果来看,向下取整,向上取整似乎问题不大,符合我们的预期,但是在四舍五入的时候,正数的四舍五入也没有问题,负数大部分也不会有问题,唯独问题在x.5这个数据上有问题,怎么会这样?

    我们怎么去记忆这个特殊情况呢?有的文章提示说关于Math.round(x)计算四舍五入,如果按照Math.floor(x+0.5)来计算,那么就没有问题了。实际上我们可以检验一下:

package com.xxx.huali.hualitest.mathmethod;

public class RoundDemo {
	private static final double a = 9.4;
	private static final double b = 9.5;
	private static final double c = 9.6;
	
	private static final double e = -9.4;
	private static final double f = -9.5;
	private static final double g = -9.6;
	public static void main(String[] args) {
		System.out.println("round(9.4) ="+Math.round(a)+"\tfloor(9.4+0.5) ="+Math.floor(a+0.5));
		System.out.println("round(9.5) ="+Math.round(b)+"\tfloor(9.5+0.5) ="+Math.floor(b+0.5));
		System.out.println("round(9.6) ="+Math.round(c)+"\tfloor(9.6+0.5) ="+Math.floor(c+0.5));
		System.out.println("round(-9.4)="+Math.round(e)+"\tfloor(-9.4+0.5)="+Math.floor(e+0.5));
		System.out.println("round(-9.5)="+Math.round(f)+"\tfloor(-9.5+0.5)="+Math.floor(f+0.5));
		System.out.println("round(-9.6)="+Math.round(g)+"\tfloor(-9.6+0.5)="+Math.floor(g+0.5));
	}
}

    运行程序,打印结果:

     我们甚至可以直接用最简单的比较法来验证:

    如果需要去记忆他们的区别,最好直接从他们的字面意思理解,对于round需要注意负数的特殊情况。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值