• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2017 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html
4 package android.icu.number;
5 
6 import android.icu.impl.number.RoundingUtils;
7 
8 /**
9  * A class that defines a rounding strategy based on a number of fraction places and optionally
10  * significant digits to be used when formatting numbers in NumberFormatter.
11  *
12  * <p>
13  * To create a FractionPrecision, use one of the factory methods on Precision.
14  *
15  * @see NumberFormatter
16  */
17 public abstract class FractionPrecision extends Precision {
18 
FractionPrecision()19     /* package-private */ FractionPrecision() {
20     }
21 
22     /**
23      * Override maximum fraction digits with maximum significant digits depending on the magnitude
24      * of the number. See UNumberRoundingPriority.
25      *
26      * @param minSignificantDigits
27      *            Pad trailing zeros to achieve this minimum number of significant digits.
28      * @param maxSignificantDigits
29      *            Round the number to achieve this maximum number of significant digits.
30      * @param priority
31      *            How to disambiguate between fraction digits and significant digits.
32      * @return A precision for chaining or passing to the NumberFormatter precision() setter.
33      */
withSignificantDigits( int minSignificantDigits, int maxSignificantDigits, NumberFormatter.RoundingPriority priority)34     public Precision withSignificantDigits(
35             int minSignificantDigits,
36             int maxSignificantDigits,
37             NumberFormatter.RoundingPriority priority) {
38         if (maxSignificantDigits >= 1 &&
39                 maxSignificantDigits >= minSignificantDigits &&
40                 maxSignificantDigits <= RoundingUtils.MAX_INT_FRAC_SIG) {
41             return constructFractionSignificant(
42                 this, minSignificantDigits, maxSignificantDigits, priority, false);
43         } else {
44             throw new IllegalArgumentException("Significant digits must be between 1 and "
45                     + RoundingUtils.MAX_INT_FRAC_SIG
46                     + " (inclusive)");
47         }
48     }
49 
50     /**
51      * Ensure that no less than this number of significant digits are retained when rounding according to
52      * fraction rules.
53      *
54      * <p>
55      * For example, with integer rounding, the number 3.141 becomes "3". However, with minimum figures
56      * set to 2, 3.141 becomes "3.1" instead.
57      *
58      * <p>
59      * This setting does not affect the number of trailing zeros. For example, 3.01 would print as "3",
60      * not "3.0".
61      *
62      * <p>
63      * This is equivalent to {@code withSignificantDigits(1, minSignificantDigits, RELAXED)}.
64      *
65      * @param minSignificantDigits
66      *            The number of significant figures to guarantee.
67      * @return A Precision for chaining or passing to the NumberFormatter rounding() setter.
68      * @throws IllegalArgumentException if the input number is too big or smaller than 1.
69      * @see NumberFormatter
70      */
withMinDigits(int minSignificantDigits)71     public Precision withMinDigits(int minSignificantDigits) {
72         if (minSignificantDigits >= 1 && minSignificantDigits <= RoundingUtils.MAX_INT_FRAC_SIG) {
73             return constructFractionSignificant(
74                 this, 1, minSignificantDigits, NumberFormatter.RoundingPriority.RELAXED, true);
75         } else {
76             throw new IllegalArgumentException("Significant digits must be between 1 and "
77                     + RoundingUtils.MAX_INT_FRAC_SIG
78                     + " (inclusive)");
79         }
80     }
81 
82     /**
83      * Ensure that no more than this number of significant digits are retained when rounding according to
84      * fraction rules.
85      *
86      * <p>
87      * For example, with integer rounding, the number 123.4 becomes "123". However, with maximum figures
88      * set to 2, 123.4 becomes "120" instead.
89      *
90      * <p>
91      * This setting does not affect the number of trailing zeros. For example, with fixed fraction of 2,
92      * 123.4 would become "120.00".
93      *
94      * <p>
95      * This is equivalent to {@code withSignificantDigits(1, maxSignificantDigits, STRICT)}.
96      *
97      * @param maxSignificantDigits
98      *            Round the number to no more than this number of significant figures.
99      * @return A Precision for chaining or passing to the NumberFormatter rounding() setter.
100      * @throws IllegalArgumentException if the input number is too big or smaller than 1.
101      * @see NumberFormatter
102      */
withMaxDigits(int maxSignificantDigits)103     public Precision withMaxDigits(int maxSignificantDigits) {
104         if (maxSignificantDigits >= 1 && maxSignificantDigits <= RoundingUtils.MAX_INT_FRAC_SIG) {
105             return constructFractionSignificant(
106                 this, 1, maxSignificantDigits, NumberFormatter.RoundingPriority.STRICT, true);
107         } else {
108             throw new IllegalArgumentException("Significant digits must be between 1 and "
109                     + RoundingUtils.MAX_INT_FRAC_SIG
110                     + " (inclusive)");
111         }
112     }
113 }
114