Rounding mode

TypeFieldSummary
static intROUND_CEILINGRound towards positive infinity.
static intROUND_DOWNRound towards zero.
static intROUND_FLOORRound towards negative infinity.
static intROUND_HALF_DOWNRound towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
static intROUND_HALF_EVENRound towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
static intROUND_HALF_UPRound towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
static intROUND_UNNECESSARYNo rounding is necessary.
static intROUND_UPRounding 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.