Package com.ibm.icu.impl.number
Class RoundingUtils
- java.lang.Object
-
- com.ibm.icu.impl.number.RoundingUtils
-
public class RoundingUtils extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static java.math.MathContext
DEFAULT_MATH_CONTEXT_34_DIGITS
The default MathContext, 34-digit version.static java.math.MathContext
DEFAULT_MATH_CONTEXT_UNLIMITED
The default MathContext, unlimited-precision version.static java.math.RoundingMode
DEFAULT_ROUNDING_MODE
The default rounding mode.private static java.math.MathContext[]
MATH_CONTEXT_BY_ROUNDING_MODE_34_DIGITS
private static java.math.MathContext[]
MATH_CONTEXT_BY_ROUNDING_MODE_UNLIMITED
static int
MAX_INT_FRAC_SIG
The maximum number of fraction places, integer numerals, or significant digits.static int
SECTION_LOWER
static int
SECTION_MIDPOINT
static int
SECTION_UPPER
-
Constructor Summary
Constructors Constructor Description RoundingUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.math.MathContext
getMathContextOr34Digits(DecimalFormatProperties properties)
Gets the user-specified math context out of the property bag.static java.math.MathContext
getMathContextOrUnlimited(DecimalFormatProperties properties)
Gets the user-specified math context out of the property bag.static StandardPlural
getPluralSafe(Precision rounder, PluralRules rules, DecimalQuantity dq)
Computes the plural form after copying the number and applying rounding rules.static boolean
getRoundingDirection(boolean isEven, boolean isNegative, int section, int roundingMode, java.lang.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.static java.math.MathContext
mathContextUnlimited(java.math.RoundingMode roundingMode)
Gets a MathContext with unlimited precision and the specified RoundingMode.static boolean
roundsAtMidpoint(int roundingMode)
Gets whether the given rounding mode's rounding boundary is at the midpoint.static Scale
scaleFromProperties(DecimalFormatProperties properties)
-
-
-
Field Detail
-
SECTION_LOWER
public static final int SECTION_LOWER
- See Also:
- Constant Field Values
-
SECTION_MIDPOINT
public static final int SECTION_MIDPOINT
- See Also:
- Constant Field Values
-
SECTION_UPPER
public static final int SECTION_UPPER
- See Also:
- Constant Field Values
-
DEFAULT_ROUNDING_MODE
public static final java.math.RoundingMode DEFAULT_ROUNDING_MODE
The default rounding mode.
-
MAX_INT_FRAC_SIG
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.- See Also:
- Constant Field Values
-
MATH_CONTEXT_BY_ROUNDING_MODE_UNLIMITED
private static final java.math.MathContext[] MATH_CONTEXT_BY_ROUNDING_MODE_UNLIMITED
-
MATH_CONTEXT_BY_ROUNDING_MODE_34_DIGITS
private static final java.math.MathContext[] MATH_CONTEXT_BY_ROUNDING_MODE_34_DIGITS
-
DEFAULT_MATH_CONTEXT_UNLIMITED
public static final java.math.MathContext DEFAULT_MATH_CONTEXT_UNLIMITED
The default MathContext, unlimited-precision version.
-
DEFAULT_MATH_CONTEXT_34_DIGITS
public static final java.math.MathContext DEFAULT_MATH_CONTEXT_34_DIGITS
The default MathContext, 34-digit version.
-
-
Method Detail
-
getRoundingDirection
public static boolean getRoundingDirection(boolean isEven, boolean isNegative, int section, int roundingMode, java.lang.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
- Whether the digit immediately before the rounding magnitude is even.isNegative
- Whether the quantity is negative.section
- 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). SeeSECTION_LOWER
,SECTION_MIDPOINT
, andSECTION_UPPER
.roundingMode
- The integer version of theRoundingMode
, which you can get viaEnum.ordinal
.reference
- A reference object to be used when throwing an ArithmeticException.- Returns:
- true if the number should be rounded toward zero; false if it should be rounded toward infinity.
-
roundsAtMidpoint
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
- The integer version of theRoundingMode
.- Returns:
- true if rounding mode is HALF_EVEN, HALF_UP, or HALF_DOWN; false otherwise.
-
getMathContextOrUnlimited
public static java.math.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
- The property bag.- Returns:
- A
MathContext
. Never null.
-
getMathContextOr34Digits
public static java.math.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
- The property bag.- Returns:
- A
MathContext
. Never null.
-
mathContextUnlimited
public static java.math.MathContext mathContextUnlimited(java.math.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
- TheRoundingMode
to use.- Returns:
- The corresponding
MathContext
.
-
scaleFromProperties
public static Scale scaleFromProperties(DecimalFormatProperties properties)
-
getPluralSafe
public static StandardPlural getPluralSafe(Precision rounder, PluralRules rules, DecimalQuantity dq)
Computes the plural form after copying the number and applying rounding rules.
-
-