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