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.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
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