FractionPrecision
abstract class FractionPrecision : Precision
A class that defines a rounding strategy based on a number of fraction places and optionally significant digits to be used when formatting numbers in NumberFormatter.
To create a FractionPrecision, use one of the factory methods on Precision.
Summary
Public methods |
open Precision! |
Ensure that no more than this number of significant digits are retained when rounding according to fraction rules.
|
open Precision! |
Ensure that no less than this number of significant digits are retained when rounding according to fraction rules.
|
open Precision! |
Override maximum fraction digits with maximum significant digits depending on the magnitude of the number.
|
Inherited functions |
From class Precision
CurrencyPrecision! |
currency(currencyUsage: Currency.CurrencyUsage!)
Show numbers rounded and padded according to the rules for the currency unit. The most common rounding precision settings for currencies include Precision.fixedFraction(2) , Precision.integer() , and Precision.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.
|
FractionPrecision! |
fixedFraction(minMaxFractionPlaces: Int)
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 with both arguments equal.
|
Precision! |
fixedSignificantDigits(minMaxSignificantDigits: Int)
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 with both arguments equal.
|
Precision! |
increment(roundingIncrement: BigDecimal!)
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 java.math.BigDecimal#scale() .
|
FractionPrecision! |
integer()
Show numbers rounded if necessary to the nearest integer.
|
FractionPrecision! |
maxFraction(maxFractionPlaces: Int)
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.
|
Precision! |
maxSignificantDigits(maxSignificantDigits: Int)
Show numbers rounded if necessary to a certain number of significant digits/figures.
|
FractionPrecision! |
minFraction(minFractionPlaces: Int)
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 .
|
FractionPrecision! |
minMaxFraction(minFractionPlaces: Int, maxFractionPlaces: Int)
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.
|
Precision! |
minMaxSignificantDigits(minSignificantDigits: Int, maxSignificantDigits: Int)
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.
|
Precision! |
minSignificantDigits(minSignificantDigits: Int)
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 .
|
Precision! |
trailingZeroDisplay(trailingZeroDisplay: NumberFormatter.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.
|
Precision! |
unlimited()
Show all available digits to full precision.
NOTE: When formatting a double, this method, along with minFraction and minSignificantDigits , 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 using maxFraction or maxSignificantDigits 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/
|
|
Public methods
withMaxDigits
open fun withMaxDigits(maxSignificantDigits: Int): Precision!
Ensure that no more than this number of significant digits are retained when rounding according to fraction rules.
For example, with integer rounding, the number 123.4 becomes "123". However, with maximum figures set to 2, 123.4 becomes "120" instead.
This setting does not affect the number of trailing zeros. For example, with fixed fraction of 2, 123.4 would become "120.00".
This is equivalent to withSignificantDigits(1, maxSignificantDigits, STRICT)
.
Parameters |
maxSignificantDigits |
Int: Round the number to no more than this number of significant figures. |
Return |
Precision! |
A Precision for chaining or passing to the NumberFormatter rounding() setter. |
Exceptions |
java.lang.IllegalArgumentException |
if the input number is too big or smaller than 1. |
withMinDigits
open fun withMinDigits(minSignificantDigits: Int): Precision!
Ensure that no less than this number of significant digits are retained when rounding according to fraction rules.
For example, with integer rounding, the number 3.141 becomes "3". However, with minimum figures set to 2, 3.141 becomes "3.1" instead.
This setting does not affect the number of trailing zeros. For example, 3.01 would print as "3", not "3.0".
This is equivalent to withSignificantDigits(1, minSignificantDigits, RELAXED)
.
Parameters |
minSignificantDigits |
Int: The number of significant figures to guarantee. |
Return |
Precision! |
A Precision for chaining or passing to the NumberFormatter rounding() setter. |
Exceptions |
java.lang.IllegalArgumentException |
if the input number is too big or smaller than 1. |
withSignificantDigits
open fun withSignificantDigits(
minSignificantDigits: Int,
maxSignificantDigits: Int,
priority: NumberFormatter.RoundingPriority!
): Precision!
Override maximum fraction digits with maximum significant digits depending on the magnitude of the number. See UNumberRoundingPriority.
Parameters |
minSignificantDigits |
Int: Pad trailing zeros to achieve this minimum number of significant digits. |
maxSignificantDigits |
Int: Round the number to achieve this maximum number of significant digits. |
priority |
NumberFormatter.RoundingPriority!: How to disambiguate between fraction digits and significant digits. |
Return |
Precision! |
A precision for chaining or passing to the NumberFormatter precision() setter. |