• 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#License
4 package ohos.global.icu.number;
5 
6 import ohos.global.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  * @hide exposed on OHOS
17  */
18 public abstract class FractionPrecision extends Precision {
19 
FractionPrecision()20     /* package-private */ FractionPrecision() {
21     }
22 
23     /**
24      * Ensure that no less than this number of significant digits are retained when rounding according to
25      * fraction rules.
26      *
27      * <p>
28      * For example, with integer rounding, the number 3.141 becomes "3". However, with minimum figures
29      * set to 2, 3.141 becomes "3.1" instead.
30      *
31      * <p>
32      * This setting does not affect the number of trailing zeros. For example, 3.01 would print as "3",
33      * not "3.0".
34      *
35      * @param minSignificantDigits
36      *            The number of significant figures to guarantee.
37      * @return A Precision for chaining or passing to the NumberFormatter rounding() setter.
38      * @throws IllegalArgumentException if the input number is too big or smaller than 1.
39      * @see NumberFormatter
40      */
withMinDigits(int minSignificantDigits)41     public Precision withMinDigits(int minSignificantDigits) {
42         if (minSignificantDigits >= 1 && minSignificantDigits <= RoundingUtils.MAX_INT_FRAC_SIG) {
43             return constructFractionSignificant(this, minSignificantDigits, -1);
44         } else {
45             throw new IllegalArgumentException("Significant digits must be between 1 and "
46                     + RoundingUtils.MAX_INT_FRAC_SIG
47                     + " (inclusive)");
48         }
49     }
50 
51     /**
52      * Ensure that no more than this number of significant digits are retained when rounding according to
53      * fraction rules.
54      *
55      * <p>
56      * For example, with integer rounding, the number 123.4 becomes "123". However, with maximum figures
57      * set to 2, 123.4 becomes "120" instead.
58      *
59      * <p>
60      * This setting does not affect the number of trailing zeros. For example, with fixed fraction of 2,
61      * 123.4 would become "120.00".
62      *
63      * @param maxSignificantDigits
64      *            Round the number to no more than this number of significant figures.
65      * @return A Precision for chaining or passing to the NumberFormatter rounding() setter.
66      * @throws IllegalArgumentException if the input number is too big or smaller than 1.
67      * @see NumberFormatter
68      */
withMaxDigits(int maxSignificantDigits)69     public Precision withMaxDigits(int maxSignificantDigits) {
70         if (maxSignificantDigits >= 1 && maxSignificantDigits <= RoundingUtils.MAX_INT_FRAC_SIG) {
71             return constructFractionSignificant(this, -1, maxSignificantDigits);
72         } else {
73             throw new IllegalArgumentException("Significant digits must be between 1 and "
74                     + RoundingUtils.MAX_INT_FRAC_SIG
75                     + " (inclusive)");
76         }
77     }
78 }