gogoWebsite

Syntax - Java rounding and other detailed explanations

Updated to 17 days ago

Java rounding and other detailed explanations

one,()

  • long round(double d)
  • int round(float f)

​ () is a classic rounding number method in Java.It returns an integer, that is, round the floating point number into an integer.

public class Main(){
    public static void main(){
        double d = 100.34;
        double e = 100.500;
        float f = 144;
        float g = 90f;
        
        System.out.println(Math.round(d));
        System.out.println(Math.round(e));
        System.out.println(Math.round(f));
        System.out.println(Math.round(g));
    }
}
result:
 100
 101
 144
 90

2. BigDecimal

​ Java's best practice is to prioritize the use of BigDecimal over() to round Java's numbers. Whenever you need to round the number to n after the decimal point, first consider BigDecimal.

    • Round away from zero direction, round to the direction with the greatest absolute value, as long as the bit is discarded, it is carried.
//
BigDecimal bigDecimal3 = new BigDecimal("2.123").setScale(1, BigDecimal.ROUND_UP);
BigDecimal bigDecimal4 = new BigDecimal("2.163").setScale(1, BigDecimal.ROUND_UP);
System.out.println("2.123=>"+bigDecimal3+"\t2.163=>"+bigDecimal4);//2.123=>2.2,2.163=>2.2
    • Round in the direction of zero, input in the direction with the smallest absolute value, and all bits must be discarded, and there is no carry situation.
//
BigDecimal bigDecimal5 = new BigDecimal("2.123").setScale(1, BigDecimal.ROUND_DOWN);
BigDecimal bigDecimal6 = new BigDecimal("2.163").setScale(1, BigDecimal.ROUND_DOWN);
System.out.println("2.123=>"+bigDecimal5+"\t2.163=>"+bigDecimal6);//2.123=>2.1,2.163=>2.1
    • Round in the direction of the positive infinite and move closer in the direction of the positive maximum.
//
BigDecimal bigDecimal7 = new BigDecimal("2.12").setScale(1, BigDecimal.ROUND_CEILING);
BigDecimal bigDecimal8 = new BigDecimal("2.19").setScale(1, BigDecimal.ROUND_CEILING);
BigDecimal bigDecimal9 = new BigDecimal("-2.12").setScale(1, BigDecimal.ROUND_CEILING);
BigDecimal bigDecimal10 = new BigDecimal("-2.19").setScale(1, BigDecimal.ROUND_CEILING);
System.out.println("2.12=>"+bigDecimal7+"\t2.19=>"+bigDecimal8+"\t-2.12=>"+bigDecimal9+"\t-2.19=>"+bigDecimal10);
//2.12=>2.2	2.19=>2.2	-2.12=>-2.1	-2.19=>-2.1
    • Round in the direction of negative infinity and move closer in the direction of negative infinity.
//
BigDecimal bigDecimal11 = new BigDecimal("2.12").setScale(1, BigDecimal.ROUND_FLOOR);
BigDecimal bigDecimal12 = new BigDecimal("2.19").setScale(1, BigDecimal.ROUND_FLOOR);
BigDecimal bigDecimal13 = new BigDecimal("-2.12").setScale(1, BigDecimal.ROUND_FLOOR);
BigDecimal bigDecimal14 = new BigDecimal("-2.19").setScale(1, BigDecimal.ROUND_FLOOR);
System.out.println("2.12=>"+bigDecimal11+"\t2.19=>"+bigDecimal12+"\t-2.12=>"+bigDecimal13+"\t-2.19=>"+bigDecimal14);
//2.12=>2.1	2.19=>2.1	-2.12=>-2.2	-2.19=>-2.2
  • round.half_up (classic rounding)

//round.half_up (classic rounding)
BigDecimal bigDecimal15 = new BigDecimal("2.12").setScale(1, BigDecimal.ROUND_HALF_UP);
BigDecimal bigDecimal16 = new BigDecimal("2.19").setScale(1, BigDecimal.ROUND_HALF_UP);
BigDecimal bigDecimal17 = new BigDecimal("-2.12").setScale(1, BigDecimal.ROUND_HALF_UP);
BigDecimal bigDecimal18 = new BigDecimal("-2.19").setScale(1, BigDecimal.ROUND_HALF_UP);
System.out.println("2.12=>"+bigDecimal15+"\t2.19=>"+bigDecimal16+"\t-2.12=>"+bigDecimal17+"\t-2.19=>"+bigDecimal18);
//2.12=>2.1	2.19=>2.2	-2.12=>-2.1	-2.19=>-2.2
  • round.half_down (rounded by six)

//round.half_down (round six)
BigDecimal bigDecimal19 = new BigDecimal("2.15").setScale(1, BigDecimal.ROUND_HALF_DOWN);
BigDecimal bigDecimal20 = new BigDecimal("2.16").setScale(1, BigDecimal.ROUND_HALF_DOWN);
BigDecimal bigDecimal21 = new BigDecimal("-2.15").setScale(1, BigDecimal.ROUND_HALF_DOWN);
BigDecimal bigDecimal22 = new BigDecimal("-2.16").setScale(1, BigDecimal.ROUND_HALF_DOWN);
System.out.println("2.15=>"+bigDecimal19+"\t2.16=>"+bigDecimal20+"\t-2.15=>"+bigDecimal21+"\t-2.16=>"+bigDecimal22);
//2.15=>2.1	2.16=>2.2	-2.15=>-2.1	-2.16=>-2.2
    • The calculation results are accurate and there is no need for rounding mode.
//
BigDecimal bigDecimal23 = new BigDecimal("2.1544").setScale( BigDecimal.ROUND_UNNECESSARY);
BigDecimal bigDecimal24 = new BigDecimal("2.1544444").setScale( BigDecimal.ROUND_UNNECESSARY);//Only accurate to 7 digits
System.out.println("2.1544=>"+bigDecimal23+"\t2.15444444=>"+bigDecimal24);

Reference Materials

3 common ways to round Java (round and retain decimals)

Java rounding