diff options
author | Elliott Hughes <enh@google.com> | 2009-12-09 00:44:31 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2009-12-09 00:44:31 -0800 |
commit | a1e5d8a2c1594f7a6ed8aca6e82b106ec8ce79d6 (patch) | |
tree | 3d10059faaea4384f64f60d35e2037da48edf963 /icu/src/main/java | |
parent | 58a69a9059cbb7efd518526cf1077603eb06241a (diff) | |
download | libcore-a1e5d8a2c1594f7a6ed8aca6e82b106ec8ce79d6.zip libcore-a1e5d8a2c1594f7a6ed8aca6e82b106ec8ce79d6.tar.gz libcore-a1e5d8a2c1594f7a6ed8aca6e82b106ec8ce79d6.tar.bz2 |
Fix java.util.Formatter formatting of -0.0.
The active ingredient here is the two changes to stop comparing longValue
with doubleValue and formatting the long if the two compare equal. This
causes us to lose the sign of 0 (because there's no long -0, but -0.0d == 0).
Instead, we explicitly test for boxed Double and Float arguments (because
the number of integral types is larger, they get the "else" clause).
The other changes are just minor cosmetic changes made as I followed the code.
Bug found by jtreg, so no new test.
Diffstat (limited to 'icu/src/main/java')
-rw-r--r-- | icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java b/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java index 104336e..81c7578 100644 --- a/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java +++ b/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java @@ -135,24 +135,20 @@ public class DecimalFormat extends NumberFormat { @Override public StringBuffer format(Object value, StringBuffer buffer, FieldPosition field) { - - if(!(value instanceof Number)) { + if (!(value instanceof Number)) { throw new IllegalArgumentException(); } - if(buffer == null || field == null) { + if (buffer == null || field == null) { throw new NullPointerException(); } - String fieldType = getFieldType(field.getFieldAttribute()); - Number number = (Number) value; - - if(number instanceof BigInteger) { + if (number instanceof BigInteger) { BigInteger valBigInteger = (BigInteger) number; - String result = NativeDecimalFormat.format(this.addr, - valBigInteger.toString(10), field, fieldType, null, 0); + String result = NativeDecimalFormat.format(this.addr, valBigInteger.toString(10), + field, fieldType, null, 0); return buffer.append(result); - } else if(number instanceof BigDecimal) { + } else if (number instanceof BigDecimal) { BigDecimal valBigDecimal = (BigDecimal) number; if (getMultiplier() != 1) { valBigDecimal = applyMultiplier(valBigDecimal); @@ -161,54 +157,39 @@ public class DecimalFormat extends NumberFormat { val.append(valBigDecimal.unscaledValue().toString(10)); int scale = valBigDecimal.scale(); scale = makeScalePositive(scale, val); - String result = NativeDecimalFormat.format(this.addr, - val.toString(), field, fieldType, null, scale); + String result = NativeDecimalFormat.format(this.addr, val.toString(), + field, fieldType, null, scale); return buffer.append(result); - } else { + } else if (number instanceof Double || number instanceof Float) { double dv = number.doubleValue(); + String result = NativeDecimalFormat.format(this.addr, dv, field, fieldType, null); + return buffer.append(result); + } else { long lv = number.longValue(); - if (dv == lv) { - String result = NativeDecimalFormat.format(this.addr, lv, field, - fieldType, null); - return buffer.append(result); - } - String result = NativeDecimalFormat.format(this.addr, dv, field, - fieldType, null); + String result = NativeDecimalFormat.format(this.addr, lv, field, fieldType, null); return buffer.append(result); } } @Override public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) { - - if(buffer == null || field == null) { + if (buffer == null || field == null) { throw new NullPointerException(); } - String fieldType = getFieldType(field.getFieldAttribute()); - - String result = NativeDecimalFormat.format(this.addr, value, field, - fieldType, null); - + String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null); buffer.append(result.toCharArray(), 0, result.length()); - return buffer; } @Override public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) { - - if(buffer == null || field == null) { + if (buffer == null || field == null) { throw new NullPointerException(); } - String fieldType = getFieldType(field.getFieldAttribute()); - - String result = NativeDecimalFormat.format(this.addr, value, field, - fieldType, null); - + String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null); buffer.append(result.toCharArray(), 0, result.length()); - return buffer; } |