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 the strategy for padding and truncating integers before
the decimal separator. | 8 * A class that defines the strategy for padding and truncating integers before
the decimal separator. |
9 * | 9 * |
10 * <p> | 10 * <p> |
11 * To create an IntegerWidth, use one of the factory methods. | 11 * To create an IntegerWidth, use one of the factory methods. |
12 * | 12 * |
13 * @draft ICU 60 | 13 * @draft ICU 60 |
14 * @provisional This API might change or be removed in a future release. | 14 * @provisional This API might change or be removed in a future release. |
15 * @see NumberFormatter | 15 * @see NumberFormatter |
16 */ | 16 */ |
17 public class IntegerWidth { | 17 public class IntegerWidth { |
18 | 18 |
19 /* package-private */ static final IntegerWidth DEFAULT = new IntegerWidth(1
, -1); | 19 /* package-private */ static final IntegerWidth DEFAULT = new IntegerWidth(1
, -1); |
20 | 20 |
21 final int minInt; | 21 final int minInt; |
22 final int maxInt; | 22 final int maxInt; |
23 | 23 |
24 private IntegerWidth(int minInt, int maxInt) { | 24 private IntegerWidth(int minInt, int maxInt) { |
25 this.minInt = minInt; | 25 this.minInt = minInt; |
26 this.maxInt = maxInt; | 26 this.maxInt = maxInt; |
27 } | 27 } |
28 | 28 |
29 /** | 29 /** |
30 * Pad numbers at the beginning with zeros to guarantee a certain number of
numerals before the decimal separator. | 30 * Pad numbers at the beginning with zeros to guarantee a certain number of
numerals before the |
| 31 * decimal separator. |
31 * | 32 * |
32 * <p> | 33 * <p> |
33 * For example, with minInt=3, the number 55 will get printed as "055". | 34 * For example, with minInt=3, the number 55 will get printed as "055". |
34 * | 35 * |
35 * @param minInt | 36 * @param minInt |
36 * The minimum number of places before the decimal separator. | 37 * The minimum number of places before the decimal separator. |
37 * @return An IntegerWidth for chaining or passing to the NumberFormatter in
tegerWidth() setter. | 38 * @return An IntegerWidth for chaining or passing to the NumberFormatter in
tegerWidth() setter. |
38 * @draft ICU 60 | 39 * @draft ICU 60 |
39 * @provisional This API might change or be removed in a future release. | 40 * @provisional This API might change or be removed in a future release. |
40 * @see NumberFormatter | 41 * @see NumberFormatter |
41 */ | 42 */ |
42 public static IntegerWidth zeroFillTo(int minInt) { | 43 public static IntegerWidth zeroFillTo(int minInt) { |
43 if (minInt == 1) { | 44 if (minInt == 1) { |
44 return DEFAULT; | 45 return DEFAULT; |
45 } else if (minInt >= 0 && minInt < RoundingUtils.MAX_INT_FRAC_SIG) { | 46 } else if (minInt >= 0 && minInt <= RoundingUtils.MAX_INT_FRAC_SIG) { |
46 return new IntegerWidth(minInt, -1); | 47 return new IntegerWidth(minInt, -1); |
47 } else { | 48 } else { |
48 throw new IllegalArgumentException( | 49 throw new IllegalArgumentException("Integer digits must be between 0
and " |
49 "Integer digits must be between 0 and " + RoundingUtils.MAX_
INT_FRAC_SIG); | 50 + RoundingUtils.MAX_INT_FRAC_SIG |
| 51 + " (inclusive)"); |
50 } | 52 } |
51 } | 53 } |
52 | 54 |
53 /** | 55 /** |
54 * Truncate numbers exceeding a certain number of numerals before the decima
l separator. | 56 * Truncate numbers exceeding a certain number of numerals before the decima
l separator. |
55 * | 57 * |
56 * For example, with maxInt=3, the number 1234 will get printed as "234". | 58 * For example, with maxInt=3, the number 1234 will get printed as "234". |
57 * | 59 * |
58 * @param maxInt | 60 * @param maxInt |
59 * The maximum number of places before the decimal separator. | 61 * The maximum number of places before the decimal separator. max
Int == -1 means no |
| 62 * truncation. |
60 * @return An IntegerWidth for passing to the NumberFormatter integerWidth()
setter. | 63 * @return An IntegerWidth for passing to the NumberFormatter integerWidth()
setter. |
61 * @draft ICU 60 | 64 * @draft ICU 60 |
62 * @provisional This API might change or be removed in a future release. | 65 * @provisional This API might change or be removed in a future release. |
63 * @see NumberFormatter | 66 * @see NumberFormatter |
64 */ | 67 */ |
65 public IntegerWidth truncateAt(int maxInt) { | 68 public IntegerWidth truncateAt(int maxInt) { |
66 if (maxInt == this.maxInt) { | 69 if (maxInt == this.maxInt) { |
67 return this; | 70 return this; |
68 } else if (maxInt >= 0 && maxInt < RoundingUtils.MAX_INT_FRAC_SIG) { | 71 } else if (maxInt >= 0 && maxInt <= RoundingUtils.MAX_INT_FRAC_SIG && ma
xInt >= minInt) { |
69 return new IntegerWidth(minInt, maxInt); | 72 return new IntegerWidth(minInt, maxInt); |
70 } else if (maxInt == -1) { | 73 } else if (maxInt == -1) { |
71 return new IntegerWidth(minInt, maxInt); | 74 return new IntegerWidth(minInt, -1); |
72 } else { | 75 } else { |
73 throw new IllegalArgumentException( | 76 throw new IllegalArgumentException("Integer digits must be between -
1 and " |
74 "Integer digits must be between 0 and " + RoundingUtils.MAX_
INT_FRAC_SIG); | 77 + RoundingUtils.MAX_INT_FRAC_SIG |
| 78 + " (inclusive)"); |
75 } | 79 } |
76 } | 80 } |
77 } | 81 } |
LEFT | RIGHT |