java Math.round()

public static void main(String[] args) throws IOException {
		
		System.out.println(Math.round(11.5));//12
		System.out.println(Math.round(-11.5));//-11
		
		System.out.println(Math.round(11.6));//12
		System.out.println(Math.round(-11.6));//-12
		
		System.out.println(Math.round(0.5));//1
		System.out.println(Math.round(-0.5));//0
	}

1、Math.round()方法是用来求四舍五入的。由上面运行结果的对比来看,除了负数有0.5(如-11.5)的情况外,其余的都是数的绝对值四舍五入,再加上相对应的正负号。

2、负数有0.5(如-11.5)的情况:

 Math.round()在API中有这样的描述:Returns the closest long to the argument, with ties rounding to positive infinity

返回的是一个最接近参数的long值,同时向正无穷方向取舍。

          Math.round(11.5) ,首先与 11.5最接近的有两个整数 11 和 12,取较大的那结果就是12;
           Math.round(-11.5),首先与 -11.5最接近的有两个整数 -11 和 -12,取较大的那结果就是-11;
           Math.round(0.5),首先与 0.5最接近的有两个整数 0 和 1,取较大的那结果就是1;
           Math.round(-0.5),首先与 -0.5最接近的有两个整数 -1 和 0,取较大的那结果就是0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值