Rounding mode

static int ROUND_CEILING
Round towards positive infinity.
static int ROUND_DOWN
Round towards zero.
static int ROUND_FLOOR
Round towards negative infinity.
static int ROUND_HALF_DOWN
Round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
static int ROUND_HALF_EVEN
Round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
static int ROUND_HALF_UP
Round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
static int ROUND_UNNECESSARY
No rounding is necessary.
static int ROUND_UP
Rounding mode to round away from zero.

import java.math.BigDecimal;
 

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(1f);
        BigDecimal second = new BigDecimal(2f);
 
        BigDecimal result1 = first.divide(second, BigDecimal.ROUND_CEILING);
        BigDecimal result2 = first.divide(second, BigDecimal.ROUND_DOWN);
        BigDecimal result3 = first.divide(second, BigDecimal.ROUND_FLOOR);
        BigDecimal result4 = first.divide(second, BigDecimal.ROUND_HALF_DOWN);
        BigDecimal result5 = first.divide(second, BigDecimal.ROUND_HALF_EVEN);
        BigDecimal result6 = first.divide(second, BigDecimal.ROUND_HALF_UP);
        BigDecimal result7 = first.divide(second, BigDecimal.ROUND_UP);
 
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
        System.out.println(result5);
        System.out.println(result6);
        System.out.println(result7);
 
    }
}

The output:


1
0
0
0
0
1
1
Home 
  Java Book 
    Essential Classes  

BigDecimal:
  1. BigDecimal class
  2. Constants for One, Ten and Zero
  3. Rounding mode
  4. Create BigDecimals
  5. Methods used to do calculation
  6. Convert BigDecimal to primitive data types
  7. Compare two BigDecimal
  8. Move decimal point
  9. Scale and precision
  10. Convert BigDecimal to String
  11. Remove the trailing zeros
  12. Convert double and long to BigDecimal
  13. Calculating Euler's number e with BigDecimal