< prev index next >

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

Print this page




 223 
 224     /**
 225      * Formats a number and appends the resulting text to the given string
 226      * buffer.
 227      * The number can be of any subclass of {@link java.lang.Number}.
 228      * <p>
 229      * This implementation extracts the number's value using
 230      * {@link java.lang.Number#longValue()} for all integral type values that
 231      * can be converted to <code>long</code> without loss of information,
 232      * including <code>BigInteger</code> values with a
 233      * {@link java.math.BigInteger#bitLength() bit length} of less than 64,
 234      * and {@link java.lang.Number#doubleValue()} for all other types. It
 235      * then calls
 236      * {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
 237      * or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
 238      * This may result in loss of magnitude information and precision for
 239      * <code>BigInteger</code> and <code>BigDecimal</code> values.
 240      * @param number     the number to format
 241      * @param toAppendTo the <code>StringBuffer</code> to which the formatted
 242      *                   text is to be appended
 243      * @param pos        On input: an alignment field, if desired.
 244      *                   On output: the offsets of the alignment field.






 245      * @return           the value passed in as <code>toAppendTo</code>
 246      * @exception        IllegalArgumentException if <code>number</code> is
 247      *                   null or not an instance of <code>Number</code>.
 248      * @exception        NullPointerException if <code>toAppendTo</code> or
 249      *                   <code>pos</code> is null
 250      * @exception        ArithmeticException if rounding is needed with rounding
 251      *                   mode being set to RoundingMode.UNNECESSARY
 252      * @see              java.text.FieldPosition
 253      */
 254     @Override
 255     public StringBuffer format(Object number,
 256                                StringBuffer toAppendTo,
 257                                FieldPosition pos) {
 258         if (number instanceof Long || number instanceof Integer ||
 259             number instanceof Short || number instanceof Byte ||
 260             number instanceof AtomicInteger || number instanceof AtomicLong ||
 261             (number instanceof BigInteger &&
 262              ((BigInteger)number).bitLength() < 64)) {
 263             return format(((Number)number).longValue(), toAppendTo, pos);
 264         } else if (number instanceof Number) {


 325    /**
 326      * Specialization of format.
 327      *
 328      * @param number the long number to format
 329      * @return the formatted String
 330      * @exception        ArithmeticException if rounding is needed with rounding
 331      *                   mode being set to RoundingMode.UNNECESSARY
 332      * @see java.text.Format#format
 333      */
 334     public final String format(long number) {
 335         return format(number, new StringBuffer(),
 336                       DontCareFieldPosition.INSTANCE).toString();
 337     }
 338 
 339    /**
 340      * Specialization of format.
 341      *
 342      * @param number     the double number to format
 343      * @param toAppendTo the StringBuffer to which the formatted text is to be
 344      *                   appended
 345      * @param pos        the field position







 346      * @return the formatted StringBuffer
 347      * @exception        ArithmeticException if rounding is needed with rounding
 348      *                   mode being set to RoundingMode.UNNECESSARY
 349      * @see java.text.Format#format
 350      */
 351     public abstract StringBuffer format(double number,
 352                                         StringBuffer toAppendTo,
 353                                         FieldPosition pos);
 354 
 355    /**
 356      * Specialization of format.
 357      *
 358      * @param number     the long number to format
 359      * @param toAppendTo the StringBuffer to which the formatted text is to be
 360      *                   appended
 361      * @param pos        the field position







 362      * @return the formatted StringBuffer
 363      * @exception        ArithmeticException if rounding is needed with rounding
 364      *                   mode being set to RoundingMode.UNNECESSARY
 365      * @see java.text.Format#format
 366      */
 367     public abstract StringBuffer format(long number,
 368                                         StringBuffer toAppendTo,
 369                                         FieldPosition pos);
 370 
 371    /**
 372      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
 373      * Long.MAX_VALUE] and with no decimals), otherwise a Double.
 374      * If IntegerOnly is set, will stop at a decimal
 375      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop
 376      * after the 1).
 377      * Does not throw an exception; if no object can be parsed, index is
 378      * unchanged!
 379      *
 380      * @param source the String to parse
 381      * @param parsePosition the parse position




 223 
 224     /**
 225      * Formats a number and appends the resulting text to the given string
 226      * buffer.
 227      * The number can be of any subclass of {@link java.lang.Number}.
 228      * <p>
 229      * This implementation extracts the number's value using
 230      * {@link java.lang.Number#longValue()} for all integral type values that
 231      * can be converted to <code>long</code> without loss of information,
 232      * including <code>BigInteger</code> values with a
 233      * {@link java.math.BigInteger#bitLength() bit length} of less than 64,
 234      * and {@link java.lang.Number#doubleValue()} for all other types. It
 235      * then calls
 236      * {@link #format(long,java.lang.StringBuffer,java.text.FieldPosition)}
 237      * or {@link #format(double,java.lang.StringBuffer,java.text.FieldPosition)}.
 238      * This may result in loss of magnitude information and precision for
 239      * <code>BigInteger</code> and <code>BigDecimal</code> values.
 240      * @param number     the number to format
 241      * @param toAppendTo the <code>StringBuffer</code> to which the formatted
 242      *                   text is to be appended
 243      * @param pos        keeps track on the position of the field within the
 244      *                   returned string. For example, for formatting a number
 245      *                   {@code 1234567.89} in {@code Locale.US} locale,
 246      *                   if the given {@code fieldPosition} is
 247      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 248      *                   and end index of {@code fieldPosition} will be set
 249      *                   to 0 and 9, respectively for the output string
 250      *                   {@code 1,234,567.89}.
 251      * @return           the value passed in as <code>toAppendTo</code>
 252      * @exception        IllegalArgumentException if <code>number</code> is
 253      *                   null or not an instance of <code>Number</code>.
 254      * @exception        NullPointerException if <code>toAppendTo</code> or
 255      *                   <code>pos</code> is null
 256      * @exception        ArithmeticException if rounding is needed with rounding
 257      *                   mode being set to RoundingMode.UNNECESSARY
 258      * @see              java.text.FieldPosition
 259      */
 260     @Override
 261     public StringBuffer format(Object number,
 262                                StringBuffer toAppendTo,
 263                                FieldPosition pos) {
 264         if (number instanceof Long || number instanceof Integer ||
 265             number instanceof Short || number instanceof Byte ||
 266             number instanceof AtomicInteger || number instanceof AtomicLong ||
 267             (number instanceof BigInteger &&
 268              ((BigInteger)number).bitLength() < 64)) {
 269             return format(((Number)number).longValue(), toAppendTo, pos);
 270         } else if (number instanceof Number) {


 331    /**
 332      * Specialization of format.
 333      *
 334      * @param number the long number to format
 335      * @return the formatted String
 336      * @exception        ArithmeticException if rounding is needed with rounding
 337      *                   mode being set to RoundingMode.UNNECESSARY
 338      * @see java.text.Format#format
 339      */
 340     public final String format(long number) {
 341         return format(number, new StringBuffer(),
 342                       DontCareFieldPosition.INSTANCE).toString();
 343     }
 344 
 345    /**
 346      * Specialization of format.
 347      *
 348      * @param number     the double number to format
 349      * @param toAppendTo the StringBuffer to which the formatted text is to be
 350      *                   appended
 351      * @param pos        keeps track on the position of the field within the
 352      *                   returned string. For example, for formatting a number
 353      *                   {@code 1234567.89} in {@code Locale.US} locale,
 354      *                   if the given {@code fieldPosition} is
 355      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 356      *                   and end index of {@code fieldPosition} will be set
 357      *                   to 0 and 9, respectively for the output string
 358      *                   {@code 1,234,567.89}.
 359      * @return the formatted StringBuffer
 360      * @exception        ArithmeticException if rounding is needed with rounding
 361      *                   mode being set to RoundingMode.UNNECESSARY
 362      * @see java.text.Format#format
 363      */
 364     public abstract StringBuffer format(double number,
 365                                         StringBuffer toAppendTo,
 366                                         FieldPosition pos);
 367 
 368    /**
 369      * Specialization of format.
 370      *
 371      * @param number     the long number to format
 372      * @param toAppendTo the StringBuffer to which the formatted text is to be
 373      *                   appended
 374      * @param pos        keeps track on the position of the field within the
 375      *                   returned string. For example, for formatting a number
 376      *                   {@code 123456789} in {@code Locale.US} locale,
 377      *                   if the given {@code fieldPosition} is
 378      *                   {@link NumberFormat#INTEGER_FIELD}, the begin index
 379      *                   and end index of {@code fieldPosition} will be set
 380      *                   to 0 and 11, respectively for the output string
 381      *                   {@code 123,456,789}.
 382      * @return the formatted StringBuffer
 383      * @exception        ArithmeticException if rounding is needed with rounding
 384      *                   mode being set to RoundingMode.UNNECESSARY
 385      * @see java.text.Format#format
 386      */
 387     public abstract StringBuffer format(long number,
 388                                         StringBuffer toAppendTo,
 389                                         FieldPosition pos);
 390 
 391    /**
 392      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,
 393      * Long.MAX_VALUE] and with no decimals), otherwise a Double.
 394      * If IntegerOnly is set, will stop at a decimal
 395      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop
 396      * after the 1).
 397      * Does not throw an exception; if no object can be parsed, index is
 398      * unchanged!
 399      *
 400      * @param source the String to parse
 401      * @param parsePosition the parse position


< prev index next >