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