LEFT | RIGHT |
(no file at all) | |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.number; | 3 package com.ibm.icu.number; |
4 | 4 |
5 import com.ibm.icu.impl.number.RoundingUtils; | 5 import com.ibm.icu.impl.number.RoundingUtils; |
6 | 6 |
7 /** | 7 /** |
8 * A class that defines a rounding strategy based on a number of fraction places
and optionally significant digits to be | 8 * A class that defines a rounding strategy based on a number of fraction places
and optionally |
9 * used when formatting numbers in NumberFormatter. | 9 * significant digits to be used when formatting numbers in NumberFormatter. |
10 * | 10 * |
11 * <p> | 11 * <p> |
12 * To create a FractionRounder, use one of the factory methods on Rounder. | 12 * To create a FractionRounder, use one of the factory methods on Rounder. |
13 * | 13 * |
14 * @draft ICU 60 | 14 * @draft ICU 60 |
15 * @provisional This API might change or be removed in a future release. | 15 * @provisional This API might change or be removed in a future release. |
16 * @see NumberFormatter | 16 * @see NumberFormatter |
17 */ | 17 */ |
18 public abstract class FractionRounder extends Rounder { | 18 public abstract class FractionRounder extends Rounder { |
19 | 19 |
20 /* package-private */ FractionRounder() { | 20 /* package-private */ FractionRounder() { |
21 } | 21 } |
22 | 22 |
23 /** | 23 /** |
24 * Ensure that no less than this number of significant digits are retained w
hen rounding according to fraction | 24 * Ensure that no less than this number of significant digits are retained w
hen rounding according to |
25 * rules. | 25 * fraction rules. |
26 * | 26 * |
27 * <p> | 27 * <p> |
28 * For example, with integer rounding, the number 3.141 becomes "3". However
, with minimum figures set to 2, 3.141 | 28 * For example, with integer rounding, the number 3.141 becomes "3". However
, with minimum figures |
29 * becomes "3.1" instead. | 29 * set to 2, 3.141 becomes "3.1" instead. |
30 * | 30 * |
31 * <p> | 31 * <p> |
32 * This setting does not affect the number of trailing zeros. For example, 3
.01 would print as "3", not "3.0". | 32 * This setting does not affect the number of trailing zeros. For example, 3
.01 would print as "3", |
| 33 * not "3.0". |
33 * | 34 * |
34 * @param minSignificantDigits | 35 * @param minSignificantDigits |
35 * The number of significant figures to guarantee. | 36 * The number of significant figures to guarantee. |
36 * @return A Rounder for chaining or passing to the NumberFormatter rounding
() setter. | 37 * @return A Rounder for chaining or passing to the NumberFormatter rounding
() setter. |
37 * @draft ICU 60 | 38 * @draft ICU 60 |
38 * @provisional This API might change or be removed in a future release. | 39 * @provisional This API might change or be removed in a future release. |
39 * @see NumberFormatter | 40 * @see NumberFormatter |
40 */ | 41 */ |
41 public Rounder withMinDigits(int minSignificantDigits) { | 42 public Rounder withMinDigits(int minSignificantDigits) { |
42 if (minSignificantDigits > 0 && minSignificantDigits <= RoundingUtils.MA
X_INT_FRAC_SIG) { | 43 if (minSignificantDigits >= 1 && minSignificantDigits <= RoundingUtils.M
AX_INT_FRAC_SIG) { |
43 return constructFractionSignificant(this, minSignificantDigits, -1); | 44 return constructFractionSignificant(this, minSignificantDigits, -1); |
44 } else { | 45 } else { |
45 throw new IllegalArgumentException( | 46 throw new IllegalArgumentException("Significant digits must be betwe
en 1 and " |
46 "Significant digits must be between 0 and " + RoundingUtils.
MAX_INT_FRAC_SIG); | 47 + RoundingUtils.MAX_INT_FRAC_SIG |
| 48 + " (inclusive)"); |
47 } | 49 } |
48 } | 50 } |
49 | 51 |
50 /** | 52 /** |
51 * Ensure that no more than this number of significant digits are retained w
hen rounding according to fraction | 53 * Ensure that no more than this number of significant digits are retained w
hen rounding according to |
52 * rules. | 54 * fraction rules. |
53 * | 55 * |
54 * <p> | 56 * <p> |
55 * For example, with integer rounding, the number 123.4 becomes "123". Howev
er, with maximum figures set to 2, 123.4 | 57 * For example, with integer rounding, the number 123.4 becomes "123". Howev
er, with maximum figures |
56 * becomes "120" instead. | 58 * set to 2, 123.4 becomes "120" instead. |
57 * | 59 * |
58 * <p> | 60 * <p> |
59 * This setting does not affect the number of trailing zeros. For example, w
ith fixed fraction of 2, 123.4 would | 61 * This setting does not affect the number of trailing zeros. For example, w
ith fixed fraction of 2, |
60 * become "120.00". | 62 * 123.4 would become "120.00". |
61 * | 63 * |
62 * @param maxSignificantDigits | 64 * @param maxSignificantDigits |
63 * Round the number to no more than this number of significant fi
gures. | 65 * Round the number to no more than this number of significant fi
gures. |
64 * @return A Rounder for chaining or passing to the NumberFormatter rounding
() setter. | 66 * @return A Rounder for chaining or passing to the NumberFormatter rounding
() setter. |
65 * @draft ICU 60 | 67 * @draft ICU 60 |
66 * @provisional This API might change or be removed in a future release. | 68 * @provisional This API might change or be removed in a future release. |
67 * @see NumberFormatter | 69 * @see NumberFormatter |
68 */ | 70 */ |
69 public Rounder withMaxDigits(int maxSignificantDigits) { | 71 public Rounder withMaxDigits(int maxSignificantDigits) { |
70 if (maxSignificantDigits > 0 && maxSignificantDigits <= RoundingUtils.MA
X_INT_FRAC_SIG) { | 72 if (maxSignificantDigits >= 1 && maxSignificantDigits <= RoundingUtils.M
AX_INT_FRAC_SIG) { |
71 return constructFractionSignificant(this, -1, maxSignificantDigits); | 73 return constructFractionSignificant(this, -1, maxSignificantDigits); |
72 } else { | 74 } else { |
73 throw new IllegalArgumentException( | 75 throw new IllegalArgumentException("Significant digits must be betwe
en 1 and " |
74 "Significant digits must be between 0 and " + RoundingUtils.
MAX_INT_FRAC_SIG); | 76 + RoundingUtils.MAX_INT_FRAC_SIG |
| 77 + " (inclusive)"); |
75 } | 78 } |
76 } | 79 } |
77 } | 80 } |
LEFT | RIGHT |