Top Fields Constructors Methods
com.ibm.icu.impl.number

public Class RoundingUtils

extends Object
Class Inheritance
Imports
java.math.BigDecimal, .MathContext, .RoundingMode, com.ibm.icu.impl.StandardPlural, com.ibm.icu.number.Precision, .Scale, com.ibm.icu.text.PluralRules

Author
sffc

Field Summary

Modifier and TypeField and Description
public static final MathContext
DEFAULT_MATH_CONTEXT_34_DIGITS

The default MathContext, 34-digit version.

public static final MathContext
DEFAULT_MATH_CONTEXT_UNLIMITED

The default MathContext, unlimited-precision version.

public static final RoundingMode
DEFAULT_ROUNDING_MODE

The default rounding mode.

private static final MathContext[]
private static final MathContext[]
public static final int
MAX_INT_FRAC_SIG

The maximum number of fraction places, integer numerals, or significant digits.

public static final int
public static final int
public static final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static MathContext

Returns:

A MathContext. Never null.
getMathContextOr34Digits
(DecimalFormatProperties
The property bag.
properties
)

Gets the user-specified math context out of the property bag.

public static MathContext

Returns:

A MathContext. Never null.
getMathContextOrUnlimited
(DecimalFormatProperties
The property bag.
properties
)

Gets the user-specified math context out of the property bag.

public static StandardPlural
getPluralSafe(Precision rounder, PluralRules rules, DecimalQuantity dq)

Deprecated as a consequence of DecimalQuantity being deprecated.
Computes the plural form after copying the number and applying rounding rules.
public static boolean

Returns:

true if the number should be rounded toward zero; false if it should be rounded toward infinity.
getRoundingDirection
(boolean
Whether the digit immediately before the rounding magnitude is even.
isEven
,
boolean
Whether the quantity is negative.
isNegative
,
int
Whether the part of the quantity to the right of the rounding magnitude is exactly halfway between two digits, whether it is in the lower part (closer to zero), or whether it is in the upper part (closer to infinity). See SECTION_LOWER, SECTION_MIDPOINT, and SECTION_UPPER.
section
,
int
The integer version of the RoundingMode, which you can get via RoundingMode#ordinal.
roundingMode
,
Object
A reference object to be used when throwing an ArithmeticException.
reference
)

Converts a rounding mode and metadata about the quantity being rounded to a boolean determining whether the value should be rounded toward infinity or toward zero.

public static MathContext

Returns:

The corresponding MathContext.
mathContextUnlimited
(RoundingMode
The RoundingMode to use.
roundingMode
)

Gets a MathContext with unlimited precision and the specified RoundingMode.

public static boolean

Returns:

true if rounding mode is HALF_EVEN, HALF_UP, or HALF_DOWN; false otherwise.
roundsAtMidpoint
(int
The integer version of the RoundingMode.
roundingMode
)

Gets whether the given rounding mode's rounding boundary is at the midpoint.

public static Scale
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_MATH_CONTEXT_34_DIGITSback to summary
public static final MathContext DEFAULT_MATH_CONTEXT_34_DIGITS

The default MathContext, 34-digit version.

DEFAULT_MATH_CONTEXT_UNLIMITEDback to summary
public static final MathContext DEFAULT_MATH_CONTEXT_UNLIMITED

The default MathContext, unlimited-precision version.

DEFAULT_ROUNDING_MODEback to summary
public static final RoundingMode DEFAULT_ROUNDING_MODE

The default rounding mode.

MATH_CONTEXT_BY_ROUNDING_MODE_34_DIGITSback to summary
private static final MathContext[] MATH_CONTEXT_BY_ROUNDING_MODE_34_DIGITS
MATH_CONTEXT_BY_ROUNDING_MODE_UNLIMITEDback to summary
private static final MathContext[] MATH_CONTEXT_BY_ROUNDING_MODE_UNLIMITED
MAX_INT_FRAC_SIGback to summary
public static final int MAX_INT_FRAC_SIG

The maximum number of fraction places, integer numerals, or significant digits.

Todo

This does not feel like the best home for this value.

SECTION_LOWERback to summary
public static final int SECTION_LOWER
SECTION_MIDPOINTback to summary
public static final int SECTION_MIDPOINT
SECTION_UPPERback to summary
public static final int SECTION_UPPER

Constructor Detail

RoundingUtilsback to summary
public RoundingUtils()

Method Detail

getMathContextOr34Digitsback to summary
public static MathContext getMathContextOr34Digits(DecimalFormatProperties properties)

Gets the user-specified math context out of the property bag. If there is none, falls back to a math context with 34 digits of precision (the 128-bit IEEE 754R default) and the user-specified rounding mode, which defaults to HALF_EVEN (the IEEE 754R default).

Parameters
properties:DecimalFormatProperties

The property bag.

Returns:MathContext

A MathContext. Never null.

getMathContextOrUnlimitedback to summary
public static MathContext getMathContextOrUnlimited(DecimalFormatProperties properties)

Gets the user-specified math context out of the property bag. If there is none, falls back to a math context with unlimited precision and the user-specified rounding mode, which defaults to HALF_EVEN (the IEEE 754R default).

Parameters
properties:DecimalFormatProperties

The property bag.

Returns:MathContext

A MathContext. Never null.

getPluralSafeback to summary
public static StandardPlural getPluralSafe(Precision rounder, PluralRules rules, DecimalQuantity dq)

Deprecated

as a consequence of DecimalQuantity being deprecated. See corresponding docs for further information.

Computes the plural form after copying the number and applying rounding rules.

getRoundingDirectionback to summary
public static boolean getRoundingDirection(boolean isEven, boolean isNegative, int section, int roundingMode, Object reference)

Converts a rounding mode and metadata about the quantity being rounded to a boolean determining whether the value should be rounded toward infinity or toward zero.

The parameters are of type int because benchmarks on an x86-64 processor against OpenJDK showed that ints were demonstrably faster than enums in switch statements.

Parameters
isEven:boolean

Whether the digit immediately before the rounding magnitude is even.

isNegative:boolean

Whether the quantity is negative.

section:int

Whether the part of the quantity to the right of the rounding magnitude is exactly halfway between two digits, whether it is in the lower part (closer to zero), or whether it is in the upper part (closer to infinity). See SECTION_LOWER, SECTION_MIDPOINT, and SECTION_UPPER.

roundingMode:int

The integer version of the RoundingMode, which you can get via RoundingMode#ordinal.

reference:Object

A reference object to be used when throwing an ArithmeticException.

Returns:boolean

true if the number should be rounded toward zero; false if it should be rounded toward infinity.

mathContextUnlimitedback to summary
public static MathContext mathContextUnlimited(RoundingMode roundingMode)

Gets a MathContext with unlimited precision and the specified RoundingMode. Equivalent to "new MathContext(0, roundingMode)", but pulls from a singleton to prevent object thrashing.

Parameters
roundingMode:RoundingMode

The RoundingMode to use.

Returns:MathContext

The corresponding MathContext.

roundsAtMidpointback to summary
public static boolean roundsAtMidpoint(int roundingMode)

Gets whether the given rounding mode's rounding boundary is at the midpoint. The rounding boundary is the point at which a number switches from being rounded down to being rounded up. For example, with rounding mode HALF_EVEN, HALF_UP, or HALF_DOWN, the rounding boundary is at the midpoint, and this function would return true. However, for UP, DOWN, CEILING, and FLOOR, the rounding boundary is at the "edge", and this function would return false.

Parameters
roundingMode:int

The integer version of the RoundingMode.

Returns:boolean

true if rounding mode is HALF_EVEN, HALF_UP, or HALF_DOWN; false otherwise.

scaleFromPropertiesback to summary
public static Scale scaleFromProperties(DecimalFormatProperties properties)