Name

RoundingMode

Synopsis

The constants defined by this enumerated type represent possible ways of rounding numbers. UP and DOWN specify rounding away from zero or toward zero. CEILING and FLOOR represent rounding toward positive infinity and negative infinity. HALF_UP, HALF_DOWN, and HALF_EVEN all round toward the nearest value and differ only in what they do when two values are equidistant. In this case, they round up, down, or to the “even” neighbor. UNNECESSARY is a special rounding mode that serves as an assertion that an arithmetic operation will have an exact result and that rounding is not needed. If this assertion fails—that is, if the operation does require rounding—an ArithmeticException is thrown.

java.math.RoundingMode

Figure 11-4. java.math.RoundingMode

public enum RoundingMode {
// Enumerated Constants
                  UP,  
     DOWN,  
     CEILING,  
     FLOOR,  
     HALF_UP,  
     HALF_DOWN,  
     HALF_EVEN,  
     UNNECESSARY;  
// Public Class Methods
     public static RoundingMode valueOf(int rm);  
     public static RoundingMode valueOf(String name);  
     public static final RoundingMode[ ] values( );  
}

Passed To

BigDecimal.{divide( ), setScale( )}, MathContext.MathContext( )

Returned By

MathContext.getRoundingMode( )

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.