java中的取整与四舍五入

public class Demo{
public static void main(String[] args){
   double i=2, j=2.1, k=2.5, m=2.9;
//   Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases:
//       •If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
//       •If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

   System.out.println("舍掉小数取整:Math.floor(2)=" + (int)Math.floor(i));
   System.out.println("舍掉小数取整:Math.floor(2.1)=" + (int)Math.floor(j));
   System.out.println("舍掉小数取整:Math.floor(2.5)=" + (int)Math.floor(k));
   System.out.println("舍掉小数取整:Math.floor(2.9)=" + (int)Math.floor(m));
                                       
   System.out.println("四舍五入取整:(2)=" + new BigDecimal("2").setScale(0, BigDecimal.ROUND_HALF_UP));
   System.out.println("四舍五入取整:(2.1)=" + new BigDecimal("2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
   System.out.println("四舍五入取整:(2.5)=" + new BigDecimal("2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
   System.out.println("四舍五入取整:(2.9)=" + new BigDecimal("2.9").setScale(0, BigDecimal.ROUND_HALF_UP));

//   Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:
//       •If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
//       •If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
//       •If the argument value is less than zero but greater than -1.0, then the result is negative zero.
//       Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
   System.out.println("凑整:Math.ceil(2)=" + (int)Math.ceil(i));
   System.out.println("凑整:Math.ceil(2.1)=" + (int)Math.ceil(j));
   System.out.println("凑整:Math.ceil(2.5)=" + (int)Math.ceil(k));
   System.out.println("凑整:Math.ceil(2.9)=" + (int)Math.ceil(m));
   
   
//   Returns the closest long to the argument, with ties rounding up.
//
//   Special cases:
//   •If the argument is NaN, the result is 0.
//   •If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
//   •If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

   System.out.println("四舍五入:Math.round(2)=" + (int)Math.round(i));
   System.out.println("四舍五入:Math.round(2.1)=" + (int)Math.round(j));
   System.out.println("四舍五入:Math.round(2.5)=" + (int)Math.round(k));
   System.out.println("四舍五入:Math.round(2.9)=" + (int)Math.round(m));

   System.out.println("舍掉小数取整:Math.floor(-2)=" + (int)Math.floor(-i));
   System.out.println("舍掉小数取整:Math.floor(-2.1)=" + (int)Math.floor(-j));
   System.out.println("舍掉小数取整:Math.floor(-2.5)=" + (int)Math.floor(-k));
   System.out.println("舍掉小数取整:Math.floor(-2.9)=" + (int)Math.floor(-m));
 
   System.out.println("四舍五入取整:(-2)=" + new BigDecimal("-2").setScale(0, BigDecimal.ROUND_HALF_UP));
   System.out.println("四舍五入取整:(-2.1)=" + new BigDecimal("-2.1").setScale(0, BigDecimal.ROUND_HALF_UP));
   System.out.println("四舍五入取整:(-2.5)=" + new BigDecimal("-2.5").setScale(0, BigDecimal.ROUND_HALF_UP));
   System.out.println("四舍五入取整:(-2.9)=" + new BigDecimal("-2.9").setScale(0, BigDecimal.ROUND_HALF_UP));

   System.out.println("凑整:Math.ceil(-2)=" + (int)Math.ceil(-i));
   System.out.println("凑整:Math.ceil(-2.1)=" + (int)Math.ceil(-j));
   System.out.println("凑整:Math.ceil(-2.5)=" + (int)Math.ceil(-k));
   System.out.println("凑整:Math.ceil(-2.9)=" + (int)Math.ceil(-m));
   }

}


结果:

舍掉小数取整:Math.floor(2)=2
舍掉小数取整:Math.floor(2.1)=2
舍掉小数取整:Math.floor(2.5)=2
舍掉小数取整:Math.floor(2.9)=2
四舍五入取整:(2)=2
四舍五入取整:(2.1)=2
四舍五入取整:(2.5)=3
四舍五入取整:(2.9)=3
凑整:Math.ceil(2)=2
凑整:Math.ceil(2.1)=3
凑整:Math.ceil(2.5)=3
凑整:Math.ceil(2.9)=3
四舍五入:Math.round(2)=2
四舍五入:Math.round(2.1)=2
四舍五入:Math.round(2.5)=3
四舍五入:Math.round(2.9)=3
舍掉小数取整:Math.floor(-2)=-2
舍掉小数取整:Math.floor(-2.1)=-3
舍掉小数取整:Math.floor(-2.5)=-3
舍掉小数取整:Math.floor(-2.9)=-3
四舍五入取整:(-2)=-2
四舍五入取整:(-2.1)=-2
四舍五入取整:(-2.5)=-3
四舍五入取整:(-2.9)=-3
凑整:Math.ceil(-2)=-2
凑整:Math.ceil(-2.1)=-2
凑整:Math.ceil(-2.5)=-2
凑整:Math.ceil(-2.9)=-2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值