< prev index next >

src/java.base/share/classes/java/text/CompactNumberFormat.java

Print this page




 167  *
 168  * <h2>Parsing</h2>
 169  * The default parsing behavior does not allow a grouping separator until
 170  * grouping used is set to {@code true} by using
 171  * {@link #setGroupingUsed(boolean)}. The parsing of the fractional part
 172  * depends on the {@link #isParseIntegerOnly()}. For example, if the
 173  * parse integer only is set to true, then the fractional part is skipped.
 174  *
 175  * <h2>Rounding</h2>
 176  * {@code CompactNumberFormat} provides rounding modes defined in
 177  * {@link java.math.RoundingMode} for formatting.  By default, it uses
 178  * {@link java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}.
 179  *
 180  * @see CompactNumberFormat.Style
 181  * @see NumberFormat
 182  * @see DecimalFormat
 183  * @since 12
 184  */
 185 public final class CompactNumberFormat extends NumberFormat {
 186 

 187     private static final long serialVersionUID = 7128367218649234678L;
 188 
 189     /**
 190      * The patterns for compact form of numbers for this
 191      * {@code CompactNumberFormat}. A possible example is
 192      * {@code {"", "", "", "0K", "00K", "000K", "0M", "00M", "000M", "0B",
 193      * "00B", "000B", "0T", "00T", "000T"}} ranging from
 194      * {@code 10}<sup>{@code 0}</sup>-{@code 10}<sup>{@code 14}</sup>,
 195      * where each pattern is used to format a range of numbers.
 196      * For example, {@code "0K"} is used for formatting
 197      * {@code number >= 1000 and number < 10000}, {@code "00K"} is used for
 198      * formatting {@code number >= 10000 and number < 100000} and so on.
 199      * This field must not be {@code null}.
 200      *
 201      * @serial
 202      */
 203     private String[] compactPatterns;
 204 
 205     /**
 206      * List of positive prefix patterns of this formatter's


1767      * {@code symbols} or {@code roundingMode} is {@code null}.
1768      * <li> If the {@code decimalPattern} or the {@code compactPatterns} array
1769      * contains an invalid pattern or if a {@code null} appears in the array of
1770      * compact patterns.
1771      * <li> If the {@code minimumIntegerDigits} is greater than the
1772      * {@code maximumIntegerDigits} or the {@code minimumFractionDigits} is
1773      * greater than the {@code maximumFractionDigits}. This check is performed
1774      * by superclass's Object.
1775      * <li> If any of the minimum/maximum integer/fraction digit count is
1776      * negative. This check is performed by superclass's readObject.
1777      * <li> If the minimum or maximum integer digit count is larger than 309 or
1778      * if the minimum or maximum fraction digit count is larger than 340.
1779      * <li> If the grouping size is negative or larger than 127.
1780      * </ul>
1781      *
1782      * @param inStream the stream
1783      * @throws IOException if an I/O error occurs
1784      * @throws ClassNotFoundException if the class of a serialized object
1785      *         could not be found
1786      */

1787     private void readObject(ObjectInputStream inStream) throws IOException,
1788             ClassNotFoundException {
1789 
1790         inStream.defaultReadObject();
1791         if (decimalPattern == null || compactPatterns == null
1792                 || symbols == null || roundingMode == null) {
1793             throw new InvalidObjectException("One of the 'decimalPattern',"
1794                     + " 'compactPatterns', 'symbols' or 'roundingMode'"
1795                     + " is null");
1796         }
1797 
1798         // Check only the maximum counts because NumberFormat.readObject has
1799         // already ensured that the maximum is greater than the minimum count.
1800         if (getMaximumIntegerDigits() > DecimalFormat.DOUBLE_INTEGER_DIGITS
1801                 || getMaximumFractionDigits() > DecimalFormat.DOUBLE_FRACTION_DIGITS) {
1802             throw new InvalidObjectException("Digit count out of range");
1803         }
1804 
1805         // Check if the grouping size is negative, on an attempt to
1806         // put value > 127, it wraps around, so check just negative value




 167  *
 168  * <h2>Parsing</h2>
 169  * The default parsing behavior does not allow a grouping separator until
 170  * grouping used is set to {@code true} by using
 171  * {@link #setGroupingUsed(boolean)}. The parsing of the fractional part
 172  * depends on the {@link #isParseIntegerOnly()}. For example, if the
 173  * parse integer only is set to true, then the fractional part is skipped.
 174  *
 175  * <h2>Rounding</h2>
 176  * {@code CompactNumberFormat} provides rounding modes defined in
 177  * {@link java.math.RoundingMode} for formatting.  By default, it uses
 178  * {@link java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}.
 179  *
 180  * @see CompactNumberFormat.Style
 181  * @see NumberFormat
 182  * @see DecimalFormat
 183  * @since 12
 184  */
 185 public final class CompactNumberFormat extends NumberFormat {
 186 
 187     @java.io.Serial
 188     private static final long serialVersionUID = 7128367218649234678L;
 189 
 190     /**
 191      * The patterns for compact form of numbers for this
 192      * {@code CompactNumberFormat}. A possible example is
 193      * {@code {"", "", "", "0K", "00K", "000K", "0M", "00M", "000M", "0B",
 194      * "00B", "000B", "0T", "00T", "000T"}} ranging from
 195      * {@code 10}<sup>{@code 0}</sup>-{@code 10}<sup>{@code 14}</sup>,
 196      * where each pattern is used to format a range of numbers.
 197      * For example, {@code "0K"} is used for formatting
 198      * {@code number >= 1000 and number < 10000}, {@code "00K"} is used for
 199      * formatting {@code number >= 10000 and number < 100000} and so on.
 200      * This field must not be {@code null}.
 201      *
 202      * @serial
 203      */
 204     private String[] compactPatterns;
 205 
 206     /**
 207      * List of positive prefix patterns of this formatter's


1768      * {@code symbols} or {@code roundingMode} is {@code null}.
1769      * <li> If the {@code decimalPattern} or the {@code compactPatterns} array
1770      * contains an invalid pattern or if a {@code null} appears in the array of
1771      * compact patterns.
1772      * <li> If the {@code minimumIntegerDigits} is greater than the
1773      * {@code maximumIntegerDigits} or the {@code minimumFractionDigits} is
1774      * greater than the {@code maximumFractionDigits}. This check is performed
1775      * by superclass's Object.
1776      * <li> If any of the minimum/maximum integer/fraction digit count is
1777      * negative. This check is performed by superclass's readObject.
1778      * <li> If the minimum or maximum integer digit count is larger than 309 or
1779      * if the minimum or maximum fraction digit count is larger than 340.
1780      * <li> If the grouping size is negative or larger than 127.
1781      * </ul>
1782      *
1783      * @param inStream the stream
1784      * @throws IOException if an I/O error occurs
1785      * @throws ClassNotFoundException if the class of a serialized object
1786      *         could not be found
1787      */
1788     @java.io.Serial
1789     private void readObject(ObjectInputStream inStream) throws IOException,
1790             ClassNotFoundException {
1791 
1792         inStream.defaultReadObject();
1793         if (decimalPattern == null || compactPatterns == null
1794                 || symbols == null || roundingMode == null) {
1795             throw new InvalidObjectException("One of the 'decimalPattern',"
1796                     + " 'compactPatterns', 'symbols' or 'roundingMode'"
1797                     + " is null");
1798         }
1799 
1800         // Check only the maximum counts because NumberFormat.readObject has
1801         // already ensured that the maximum is greater than the minimum count.
1802         if (getMaximumIntegerDigits() > DecimalFormat.DOUBLE_INTEGER_DIGITS
1803                 || getMaximumFractionDigits() > DecimalFormat.DOUBLE_FRACTION_DIGITS) {
1804             throw new InvalidObjectException("Digit count out of range");
1805         }
1806 
1807         // Check if the grouping size is negative, on an attempt to
1808         // put value > 127, it wraps around, so check just negative value


< prev index next >