@@ -313,20 +313,30 @@ public int hashCode()
313
313
* @return The String representation of this long.
314
314
*/
315
315
public final StringBuffer format (Object obj , StringBuffer sbuf , FieldPosition pos )
316
+ {
317
+ return format (obj , sbuf , pos , false );
318
+ }
319
+
320
+ private StringBuffer format (Object obj , StringBuffer sbuf , FieldPosition pos ,
321
+ boolean addAttrs )
316
322
{
317
323
if (obj instanceof BigInteger )
318
324
{
319
325
BigDecimal decimal = new BigDecimal ((BigInteger ) obj );
320
- formatInternal (decimal , true , sbuf , pos );
326
+ formatInternal (decimal , true , sbuf , pos , addAttrs );
321
327
return sbuf ;
322
328
}
323
329
else if (obj instanceof BigDecimal )
324
330
{
325
- formatInternal ((BigDecimal ) obj , true , sbuf , pos );
331
+ formatInternal ((BigDecimal ) obj , false , sbuf , pos , addAttrs );
326
332
return sbuf ;
327
333
}
334
+ else if (obj instanceof Number )
335
+ {
336
+ return format (((Number ) obj ).doubleValue (), sbuf , pos , addAttrs );
337
+ }
328
338
329
- return super . format ( obj , sbuf , pos );
339
+ throw new IllegalArgumentException ( "Cannot format given Object as a Number" );
330
340
}
331
341
332
342
/**
@@ -341,6 +351,12 @@ else if (obj instanceof BigDecimal)
341
351
*/
342
352
public StringBuffer format (double number , StringBuffer dest ,
343
353
FieldPosition fieldPos )
354
+ {
355
+ return format (number , dest , fieldPos , false );
356
+ }
357
+
358
+ private StringBuffer format (double number , StringBuffer dest ,
359
+ FieldPosition fieldPos , boolean addAttrs )
344
360
{
345
361
// special cases for double: NaN and negative or positive infinity
346
362
if (Double .isNaN (number ))
@@ -384,7 +400,7 @@ else if (Double.isInfinite(number))
384
400
{
385
401
// get the number as a BigDecimal
386
402
BigDecimal bigDecimal = new BigDecimal (String .valueOf (number ));
387
- formatInternal (bigDecimal , false , dest , fieldPos );
403
+ formatInternal (bigDecimal , false , dest , fieldPos , addAttrs );
388
404
}
389
405
390
406
return dest ;
@@ -401,9 +417,15 @@ else if (Double.isInfinite(number))
401
417
*/
402
418
public StringBuffer format (long number , StringBuffer dest ,
403
419
FieldPosition fieldPos )
420
+ {
421
+ return format (number , dest , fieldPos , false );
422
+ }
423
+
424
+ private StringBuffer format (long number , StringBuffer dest ,
425
+ FieldPosition fieldPos , boolean addAttrs )
404
426
{
405
427
BigDecimal bigDecimal = new BigDecimal (String .valueOf (number ));
406
- formatInternal (bigDecimal , true , dest , fieldPos );
428
+ formatInternal (bigDecimal , true , dest , fieldPos , addAttrs );
407
429
return dest ;
408
430
}
409
431
@@ -430,9 +452,7 @@ public AttributedCharacterIterator formatToCharacterIterator(Object value)
430
452
IllegalArgumentException ("Cannot format given Object as a Number" );
431
453
432
454
StringBuffer text = new StringBuffer ();
433
- attributes .clear ();
434
- super .format (value , text , new FieldPosition (0 ));
435
-
455
+ format (value , text , new FieldPosition (0 ), true );
436
456
AttributedString as = new AttributedString (text .toString ());
437
457
438
458
// add NumberFormat field attributes to the AttributedString
@@ -444,6 +464,7 @@ public AttributedCharacterIterator formatToCharacterIterator(Object value)
444
464
as .addAttribute (attribute , attribute , pos .getBeginIndex (),
445
465
pos .getEndIndex ());
446
466
}
467
+ attributes .clear ();
447
468
448
469
// return the CharacterIterator from AttributedString
449
470
return as .getIterator ();
@@ -1764,9 +1785,10 @@ else if (ch == patternSeparator)
1764
1785
* @param isLong A boolean that indicates if this BigDecimal is a real
1765
1786
* decimal or an integer.
1766
1787
* @param fieldPos Use to keep track of the formatting position.
1788
+ * @param addAttrs If true, attribute information will be recorded.
1767
1789
*/
1768
1790
private void formatInternal (BigDecimal number , boolean isLong ,
1769
- StringBuffer dest , FieldPosition fieldPos )
1791
+ StringBuffer dest , FieldPosition fieldPos , boolean addAttrs )
1770
1792
{
1771
1793
// The specs says that fieldPos should not be null, and that we
1772
1794
// should throw a NPE, but it seems that in few classes that
@@ -1798,7 +1820,8 @@ private void formatInternal(BigDecimal number, boolean isLong,
1798
1820
1799
1821
_multiplier = negativePatternMultiplier ;
1800
1822
1801
- addAttribute (Field .SIGN , attributeStart , dest .length ());
1823
+ if (addAttrs )
1824
+ addAttribute (Field .SIGN , attributeStart , dest .length ());
1802
1825
}
1803
1826
else
1804
1827
{
@@ -1930,7 +1953,8 @@ private void formatInternal(BigDecimal number, boolean isLong,
1930
1953
}
1931
1954
1932
1955
// add the INTEGER attribute
1933
- addAttribute (Field .INTEGER , attributeStart , dest .length ());
1956
+ if (addAttrs )
1957
+ addAttribute (Field .INTEGER , attributeStart , dest .length ());
1934
1958
1935
1959
// ...update field position, if needed, and return...
1936
1960
if ((fieldPos .getField () == INTEGER_FIELD ||
@@ -1940,7 +1964,7 @@ private void formatInternal(BigDecimal number, boolean isLong,
1940
1964
fieldPos .setEndIndex (endIndexInt );
1941
1965
}
1942
1966
1943
- handleFractionalPart (dest , fractPart , fieldPos , isLong );
1967
+ handleFractionalPart (dest , fractPart , fieldPos , isLong , addAttrs );
1944
1968
1945
1969
// and the exponent
1946
1970
if (this .useExponentialNotation )
@@ -1949,15 +1973,17 @@ private void formatInternal(BigDecimal number, boolean isLong,
1949
1973
1950
1974
dest .append (symbols .getExponential ());
1951
1975
1952
- addAttribute (Field .EXPONENT_SYMBOL , attributeStart , dest .length ());
1976
+ if (addAttrs )
1977
+ addAttribute (Field .EXPONENT_SYMBOL , attributeStart , dest .length ());
1953
1978
attributeStart = dest .length ();
1954
1979
1955
1980
if (exponent < 0 )
1956
1981
{
1957
1982
dest .append (symbols .getMinusSign ());
1958
1983
exponent = -exponent ;
1959
1984
1960
- addAttribute (Field .EXPONENT_SIGN , attributeStart , dest .length ());
1985
+ if (addAttrs )
1986
+ addAttribute (Field .EXPONENT_SIGN , attributeStart , dest .length ());
1961
1987
}
1962
1988
1963
1989
attributeStart = dest .length ();
@@ -1971,7 +1997,8 @@ private void formatInternal(BigDecimal number, boolean isLong,
1971
1997
for (int i = 0 ; i < exponentLength ; ++i )
1972
1998
dest .append (exponentString .charAt (i ));
1973
1999
1974
- addAttribute (Field .EXPONENT , attributeStart , dest .length ());
2000
+ if (addAttrs )
2001
+ addAttribute (Field .EXPONENT , attributeStart , dest .length ());
1975
2002
}
1976
2003
1977
2004
// now include the suffixes...
@@ -1993,9 +2020,10 @@ private void formatInternal(BigDecimal number, boolean isLong,
1993
2020
* @param fractPart
1994
2021
* @param fieldPos
1995
2022
* @param isLong
2023
+ * @param addAttrs
1996
2024
*/
1997
2025
private void handleFractionalPart (StringBuffer dest , String fractPart ,
1998
- FieldPosition fieldPos , boolean isLong )
2026
+ FieldPosition fieldPos , boolean isLong , boolean addAttrs )
1999
2027
{
2000
2028
int dotStart = 0 ;
2001
2029
int dotEnd = 0 ;
@@ -2071,10 +2099,10 @@ else if (!this.decimalSeparatorAlwaysShown)
2071
2099
}
2072
2100
}
2073
2101
2074
- if (addDecimal )
2102
+ if (addAttrs && addDecimal )
2075
2103
addAttribute (Field .DECIMAL_SEPARATOR , dotStart , dotEnd );
2076
2104
2077
- if (addFractional )
2105
+ if (addAttrs && addFractional )
2078
2106
addAttribute (Field .FRACTION , fractStart , fractEnd );
2079
2107
2080
2108
if ((fieldPos .getField () == FRACTION_FIELD ||
0 commit comments