Class Precision
- java.lang.Object
-
- com.ibm.icu.number.Precision
-
- Direct Known Subclasses:
CurrencyPrecision
,FractionPrecision
,Precision.BogusRounder
,Precision.FracSigRounderImpl
,Precision.IncrementRounderImpl
,Precision.InfiniteRounderImpl
,Precision.SignificantRounderImpl
public abstract class Precision extends java.lang.Object
A class that defines the rounding precision to be used when formatting numbers in NumberFormatter.To create a Precision, use one of the factory methods.
- See Also:
NumberFormatter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Precision.BogusRounder
Deprecated.This API is ICU internal only.(package private) static class
Precision.CurrencyRounderImpl
(package private) static class
Precision.FracSigRounderImpl
(package private) static class
Precision.FractionRounderImpl
(package private) static class
Precision.IncrementFiveRounderImpl
Used for increments with 5 as the only digit (nickel rounding).(package private) static class
Precision.IncrementOneRounderImpl
Used for increments with 1 as the only digit.(package private) static class
Precision.IncrementRounderImpl
Used for strange increments like 3.14.(package private) static class
Precision.InfiniteRounderImpl
(package private) static class
Precision.SignificantRounderImpl
-
Field Summary
-
Constructor Summary
Constructors Constructor Description Precision()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract void
apply(DecimalQuantity value)
Deprecated.ICU 60 This API is ICU internal only.(package private) int
chooseMultiplierAndApply(DecimalQuantity input, MultiplierProducer producer)
Rounding endpoint used by Engineering and Compact notation.(package private) static CurrencyPrecision
constructCurrency(Currency.CurrencyUsage usage)
(package private) static FractionPrecision
constructFraction(int minFrac, int maxFrac)
(package private) static Precision
constructFractionSignificant(FractionPrecision base_, int minSig, int maxSig, NumberFormatter.RoundingPriority priority, boolean retain)
(package private) static Precision
constructFromCurrency(CurrencyPrecision base_, Currency currency)
(package private) static Precision
constructIncrement(java.math.BigDecimal increment)
(package private) static Precision
constructInfinite()
(package private) static Precision
constructSignificant(int minSig, int maxSig)
Assumes that minSig <= maxSig.(package private) abstract Precision
createCopy()
Package-private clone method(package private) void
createCopyHelper(Precision copy)
Call this function to copy the fields from the Precision base class.static CurrencyPrecision
currency(Currency.CurrencyUsage currencyUsage)
Show numbers rounded and padded according to the rules for the currency unit.static FractionPrecision
fixedFraction(int minMaxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator).static Precision
fixedSignificantDigits(int minMaxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits or significant figures.private static int
getDisplayMagnitudeFraction(int minFrac)
private static int
getDisplayMagnitudeSignificant(DecimalQuantity value, int minSig)
private static int
getRoundingMagnitudeFraction(int maxFrac)
private static int
getRoundingMagnitudeSignificant(DecimalQuantity value, int maxSig)
static Precision
increment(java.math.BigDecimal roundingIncrement)
Show numbers rounded if necessary to the closest multiple of a certain rounding increment.static FractionPrecision
integer()
Show numbers rounded if necessary to the nearest integer.static FractionPrecision
maxFraction(int maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator).static Precision
maxSignificantDigits(int maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures.static FractionPrecision
minFraction(int minFractionPlaces)
Always show at least a certain number of fraction places after the decimal separator, padding with zeros if necessary.static FractionPrecision
minMaxFraction(int minFractionPlaces, int maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator); in addition, always show at least a certain number of places after the decimal separator, padding with zeros if necessary.static Precision
minMaxSignificantDigits(int minSignificantDigits, int maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures; in addition, always show at least a certain number of significant digits, padding with zeros if necessary.static Precision
minSignificantDigits(int minSignificantDigits)
Always show at least a certain number of significant digits/figures, padding with zeros if necessary.(package private) void
setResolvedMinFraction(DecimalQuantity value, int resolvedMinFraction)
Precision
trailingZeroDisplay(NumberFormatter.TrailingZeroDisplay trailingZeroDisplay)
Configure how trailing zeros are displayed on numbers.static Precision
unlimited()
Show all available digits to full precision.(package private) Precision
withLocaleData(Currency currency)
Returns a valid working Rounder.Precision
withMode(java.math.MathContext mathContext)
Deprecated.This API is ICU internal only.
-
-
-
Field Detail
-
mathContext
java.math.MathContext mathContext
-
trailingZeroDisplay
NumberFormatter.TrailingZeroDisplay trailingZeroDisplay
-
BOGUS_PRECISION
@Deprecated public static final Precision.BogusRounder BOGUS_PRECISION
Deprecated.ICU internal only.
-
NONE
static final Precision.InfiniteRounderImpl NONE
-
FIXED_FRAC_0
static final Precision.FractionRounderImpl FIXED_FRAC_0
-
FIXED_FRAC_2
static final Precision.FractionRounderImpl FIXED_FRAC_2
-
DEFAULT_MAX_FRAC_6
static final Precision.FractionRounderImpl DEFAULT_MAX_FRAC_6
-
FIXED_SIG_2
static final Precision.SignificantRounderImpl FIXED_SIG_2
-
FIXED_SIG_3
static final Precision.SignificantRounderImpl FIXED_SIG_3
-
RANGE_SIG_2_3
static final Precision.SignificantRounderImpl RANGE_SIG_2_3
-
COMPACT_STRATEGY
static final Precision.FracSigRounderImpl COMPACT_STRATEGY
-
NICKEL
static final Precision.IncrementFiveRounderImpl NICKEL
-
MONETARY_STANDARD
static final Precision.CurrencyRounderImpl MONETARY_STANDARD
-
MONETARY_CASH
static final Precision.CurrencyRounderImpl MONETARY_CASH
-
-
Method Detail
-
unlimited
public static Precision unlimited()
Show all available digits to full precision.NOTE: When formatting a double, this method, along with
minFraction(int)
andminSignificantDigits(int)
, will trigger complex algorithm similar to Dragon4 to determine the low-order digits and the number of digits to display based on the value of the double. If the number of fraction places or significant digits can be bounded, consider usingmaxFraction(int)
ormaxSignificantDigits(int)
instead to maximize performance. For more information, read the following blog post.http://www.serpentine.com/blog/2011/06/29/here-be-dragons-advances-in-problems-you-didnt-even-know-you-had/
- Returns:
- A Precision for chaining or passing to the NumberFormatter precision() setter.
- See Also:
NumberFormatter
-
integer
public static FractionPrecision integer()
Show numbers rounded if necessary to the nearest integer.- Returns:
- A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
- See Also:
NumberFormatter
-
fixedFraction
public static FractionPrecision fixedFraction(int minMaxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator). Additionally, pad with zeros to ensure that this number of places are always shown.Example output with minMaxFractionPlaces = 3:
87,650.000
8,765.000
876.500
87.650
8.765
0.876
0.088
0.009
0.000 (zero)This method is equivalent to
minMaxFraction(int, int)
with both arguments equal.- Parameters:
minMaxFractionPlaces
- The minimum and maximum number of numerals to display after the decimal separator (rounding if too long or padding with zeros if too short).- Returns:
- A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 0.- See Also:
NumberFormatter
-
minFraction
public static FractionPrecision minFraction(int minFractionPlaces)
Always show at least a certain number of fraction places after the decimal separator, padding with zeros if necessary. Do not perform rounding (display numbers to their full precision).NOTE: If you are formatting doubles, see the performance note in
unlimited()
.- Parameters:
minFractionPlaces
- The minimum number of numerals to display after the decimal separator (padding with zeros if necessary).- Returns:
- A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 0.- See Also:
NumberFormatter
-
maxFraction
public static FractionPrecision maxFraction(int maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator). Unlike the other fraction rounding strategies, this strategy does not pad zeros to the end of the number.- Parameters:
maxFractionPlaces
- The maximum number of numerals to display after the decimal mark (rounding if necessary).- Returns:
- A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 0.- See Also:
NumberFormatter
-
minMaxFraction
public static FractionPrecision minMaxFraction(int minFractionPlaces, int maxFractionPlaces)
Show numbers rounded if necessary to a certain number of fraction places (numerals after the decimal separator); in addition, always show at least a certain number of places after the decimal separator, padding with zeros if necessary.- Parameters:
minFractionPlaces
- The minimum number of numerals to display after the decimal separator (padding with zeros if necessary).maxFractionPlaces
- The maximum number of numerals to display after the decimal separator (rounding if necessary).- Returns:
- A FractionPrecision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 0.- See Also:
NumberFormatter
-
fixedSignificantDigits
public static Precision fixedSignificantDigits(int minMaxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits or significant figures. Additionally, pad with zeros to ensure that this number of significant digits/figures are always shown.This method is equivalent to
minMaxSignificantDigits(int, int)
with both arguments equal.- Parameters:
minMaxSignificantDigits
- The minimum and maximum number of significant digits to display (rounding if too long or padding with zeros if too short).- Returns:
- A Precision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 1.- See Also:
NumberFormatter
-
minSignificantDigits
public static Precision minSignificantDigits(int minSignificantDigits)
Always show at least a certain number of significant digits/figures, padding with zeros if necessary. Do not perform rounding (display numbers to their full precision).NOTE: If you are formatting doubles, see the performance note in
unlimited()
.- Parameters:
minSignificantDigits
- The minimum number of significant digits to display (padding with zeros if too short).- Returns:
- A Precision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 1.- See Also:
NumberFormatter
-
maxSignificantDigits
public static Precision maxSignificantDigits(int maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures.- Parameters:
maxSignificantDigits
- The maximum number of significant digits to display (rounding if too long).- Returns:
- A Precision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 1.- See Also:
NumberFormatter
-
minMaxSignificantDigits
public static Precision minMaxSignificantDigits(int minSignificantDigits, int maxSignificantDigits)
Show numbers rounded if necessary to a certain number of significant digits/figures; in addition, always show at least a certain number of significant digits, padding with zeros if necessary.- Parameters:
minSignificantDigits
- The minimum number of significant digits to display (padding with zeros if necessary).maxSignificantDigits
- The maximum number of significant digits to display (rounding if necessary).- Returns:
- A Precision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the input number is too big or smaller than 1.- See Also:
NumberFormatter
-
increment
public static Precision increment(java.math.BigDecimal roundingIncrement)
Show numbers rounded if necessary to the closest multiple of a certain rounding increment. For example, if the rounding increment is 0.5, then round 1.2 to 1 and round 1.3 to 1.5.In order to ensure that numbers are padded to the appropriate number of fraction places, set the scale on the rounding increment BigDecimal. For example, to round to the nearest 0.5 and always display 2 numerals after the decimal separator (to display 1.2 as "1.00" and 1.3 as "1.50"), you can run:
Precision.increment(new BigDecimal("0.50"))
For more information on the scale of Java BigDecimal, see
BigDecimal.scale()
.- Parameters:
roundingIncrement
- The increment to which to round numbers.- Returns:
- A Precision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if the rounding increment is null or non-positive.- See Also:
NumberFormatter
-
currency
public static CurrencyPrecision currency(Currency.CurrencyUsage currencyUsage)
Show numbers rounded and padded according to the rules for the currency unit. The most common rounding precision settings for currencies includePrecision.fixedFraction(2)
,Precision.integer()
, andPrecision.increment(0.05)
for cash transactions ("nickel rounding").The exact rounding details will be resolved at runtime based on the currency unit specified in the NumberFormatter chain. To round according to the rules for one currency while displaying the symbol for another currency, the withCurrency() method can be called on the return value of this method.
- Parameters:
currencyUsage
- Either STANDARD (for digital transactions) or CASH (for transactions where the rounding increment may be limited by the available denominations of cash or coins).- Returns:
- A CurrencyPrecision for chaining or passing to the NumberFormatter precision() setter.
- Throws:
java.lang.IllegalArgumentException
- if currencyUsage is null.- See Also:
NumberFormatter
-
trailingZeroDisplay
public Precision trailingZeroDisplay(NumberFormatter.TrailingZeroDisplay trailingZeroDisplay)
Configure how trailing zeros are displayed on numbers. For example, to hide trailing zeros when the number is an integer, use HIDE_IF_WHOLE.- Parameters:
trailingZeroDisplay
- Option to configure the display of trailing zeros.
-
withMode
@Deprecated public Precision withMode(java.math.MathContext mathContext)
Deprecated.This API is ICU internal only.Sets a MathContext to use on this Precision.
-
createCopy
abstract Precision createCopy()
Package-private clone method
-
createCopyHelper
void createCopyHelper(Precision copy)
Call this function to copy the fields from the Precision base class. Note: It would be nice if this returned the copy, but most impls return the child class, not Precision.
-
apply
@Deprecated public abstract void apply(DecimalQuantity value)
Deprecated.ICU 60 This API is ICU internal only.
-
constructInfinite
static Precision constructInfinite()
-
constructFraction
static FractionPrecision constructFraction(int minFrac, int maxFrac)
-
constructSignificant
static Precision constructSignificant(int minSig, int maxSig)
Assumes that minSig <= maxSig.
-
constructFractionSignificant
static Precision constructFractionSignificant(FractionPrecision base_, int minSig, int maxSig, NumberFormatter.RoundingPriority priority, boolean retain)
-
constructIncrement
static Precision constructIncrement(java.math.BigDecimal increment)
-
constructCurrency
static CurrencyPrecision constructCurrency(Currency.CurrencyUsage usage)
-
constructFromCurrency
static Precision constructFromCurrency(CurrencyPrecision base_, Currency currency)
-
withLocaleData
Precision withLocaleData(Currency currency)
Returns a valid working Rounder. If the Rounder is a CurrencyRounder, applies the given currency. Otherwise, simply passes through the argument.- Parameters:
currency
- A currency object to use in case the input object needs it.- Returns:
- A Rounder object ready for use.
-
chooseMultiplierAndApply
int chooseMultiplierAndApply(DecimalQuantity input, MultiplierProducer producer)
Rounding endpoint used by Engineering and Compact notation. Chooses the most appropriate multiplier (magnitude adjustment), applies the adjustment, rounds, and returns the chosen multiplier.In most cases, this is simple. However, when rounding the number causes it to cross a multiplier boundary, we need to re-do the rounding. For example, to display 999,999 in Engineering notation with 2 sigfigs, first you guess the multiplier to be -3. However, then you end up getting 1000E3, which is not the correct output. You then change your multiplier to be -6, and you get 1.0E6, which is correct.
- Parameters:
input
- The quantity to process.producer
- Function to call to return a multiplier based on a magnitude.- Returns:
- The number of orders of magnitude the input was adjusted by this method.
-
getRoundingMagnitudeFraction
private static int getRoundingMagnitudeFraction(int maxFrac)
-
getRoundingMagnitudeSignificant
private static int getRoundingMagnitudeSignificant(DecimalQuantity value, int maxSig)
-
getDisplayMagnitudeFraction
private static int getDisplayMagnitudeFraction(int minFrac)
-
setResolvedMinFraction
void setResolvedMinFraction(DecimalQuantity value, int resolvedMinFraction)
-
getDisplayMagnitudeSignificant
private static int getDisplayMagnitudeSignificant(DecimalQuantity value, int minSig)
-
-