diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-02-13 12:57:48 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-02-13 12:57:48 -0800 |
commit | 9cb89d78930c6ff1f18ab5e3eea393a47e9fff4d (patch) | |
tree | e03c4748ec34c1ace469c8f00ef6ee403aeb4966 /icu/src/main/java/com | |
parent | b7926325a1c1a370c84c81db80372f59af240a53 (diff) | |
download | libcore-9cb89d78930c6ff1f18ab5e3eea393a47e9fff4d.zip libcore-9cb89d78930c6ff1f18ab5e3eea393a47e9fff4d.tar.gz libcore-9cb89d78930c6ff1f18ab5e3eea393a47e9fff4d.tar.bz2 |
auto import from //branches/cupcake/...@131421
Diffstat (limited to 'icu/src/main/java/com')
-rw-r--r-- | icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java | 127 |
1 files changed, 72 insertions, 55 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 ddd4062..5bee1fd 100644 --- a/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java +++ b/icu/src/main/java/com/ibm/icu4jni/text/DecimalFormat.java @@ -31,12 +31,12 @@ import java.util.Currency; import java.util.Locale; public class DecimalFormat extends NumberFormat { - + private int addr; - + private DecimalFormatSymbols symbols; - - // fix to be icu4j conform (harmony wants this field to exist) + + // fix to be icu4j conform (harmony wants this field to exist) // for serialization of java.text.DecimalFormat @SuppressWarnings("unused") private boolean useExponentialNotation = false; @@ -47,7 +47,7 @@ public class DecimalFormat extends NumberFormat { private boolean negSuffNull; private boolean posPrefNull; private boolean posSuffNull; - + public DecimalFormat(String pattern, DecimalFormatSymbols icuSymbols) { this.addr = icuSymbols.getAddr(); this.symbols = icuSymbols; @@ -58,7 +58,7 @@ public class DecimalFormat extends NumberFormat { public int hashCode() { return super.hashCode() * 37 + this.getPositivePrefix().hashCode(); } - + @Override public Object clone() { String pat = this.toPattern(); @@ -73,7 +73,7 @@ public class DecimalFormat extends NumberFormat { newdf.setGroupingSize(this.getGroupingSize()); return newdf; } - + @Override public boolean equals(Object object) { if (object == this) { @@ -87,10 +87,10 @@ public class DecimalFormat extends NumberFormat { if(obj.addr == this.addr) { return true; } - + boolean result = super.equals(object); - + result &= obj.toPattern().equals(this.toPattern()); result &= obj.isDecimalSeparatorAlwaysShown() == this.isDecimalSeparatorAlwaysShown(); result &= obj.getGroupingSize() == this.getGroupingSize(); @@ -114,34 +114,37 @@ public class DecimalFormat extends NumberFormat { result &= thisCurr == null; } result &= obj.getDecimalFormatSymbols().equals(this.getDecimalFormatSymbols()); - + return result; } @Override public StringBuffer format(Object value, StringBuffer buffer, FieldPosition field) { - + if(!(value instanceof Number)) { throw new IllegalArgumentException(); } if(buffer == null || field == null) { throw new NullPointerException(); } - + String fieldType = getFieldType(field.getFieldAttribute()); - + Number number = (Number) value; - + if(number instanceof BigInteger) { BigInteger valBigInteger = (BigInteger) number; - String result = NativeDecimalFormat.format(this.addr, + String result = NativeDecimalFormat.format(this.addr, valBigInteger.toString(10), field, fieldType, null, 0); return buffer.append(result); } else if(number instanceof BigDecimal) { BigDecimal valBigDecimal = (BigDecimal) number; - String result = NativeDecimalFormat.format(this.addr, - valBigDecimal.unscaledValue().toString(10), field, - fieldType, null, valBigDecimal.scale()); + StringBuilder val = new StringBuilder(); + 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); return buffer.append(result); } else { double dv = number.doubleValue(); @@ -151,7 +154,7 @@ public class DecimalFormat extends NumberFormat { fieldType, null); return buffer.append(result); } - String result = NativeDecimalFormat.format(this.addr, dv, field, + String result = NativeDecimalFormat.format(this.addr, dv, field, fieldType, null); return buffer.append(result); } @@ -163,14 +166,14 @@ public class DecimalFormat extends NumberFormat { if(buffer == null || field == null) { throw new NullPointerException(); } - + String fieldType = getFieldType(field.getFieldAttribute()); - - String result = NativeDecimalFormat.format(this.addr, value, field, + + String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null); - + buffer.append(result.toCharArray(), 0, result.length()); - + return buffer; } @@ -180,14 +183,14 @@ public class DecimalFormat extends NumberFormat { if(buffer == null || field == null) { throw new NullPointerException(); } - + String fieldType = getFieldType(field.getFieldAttribute()); - - String result = NativeDecimalFormat.format(this.addr, value, field, + + String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null); - + buffer.append(result.toCharArray(), 0, result.length()); - + return buffer; } @@ -223,16 +226,19 @@ public class DecimalFormat extends NumberFormat { Number number = (Number) object; String text = null; StringBuffer attributes = new StringBuffer(); - + if(number instanceof BigInteger) { BigInteger valBigInteger = (BigInteger) number; - text = NativeDecimalFormat.format(this.addr, + text = NativeDecimalFormat.format(this.addr, valBigInteger.toString(10), null, null, attributes, 0); } else if(number instanceof BigDecimal) { BigDecimal valBigDecimal = (BigDecimal) number; - text = NativeDecimalFormat.format(this.addr, - valBigDecimal.unscaledValue().toString(10), null,null, - attributes, valBigDecimal.scale()); + StringBuilder val = new StringBuilder(); + val.append(valBigDecimal.unscaledValue().toString(10)); + int scale = valBigDecimal.scale(); + scale = makeScalePositive(scale, val); + text = NativeDecimalFormat.format(this.addr, val.toString(), null, + null, attributes, scale); } else { double dv = number.doubleValue(); long lv = number.longValue(); @@ -240,11 +246,11 @@ public class DecimalFormat extends NumberFormat { text = NativeDecimalFormat.format(this.addr, lv, null, null, attributes); } else { - text = NativeDecimalFormat.format(this.addr, dv, null, + text = NativeDecimalFormat.format(this.addr, dv, null, null, attributes); } } - + AttributedString as = new AttributedString(text.toString()); String[] attrs = attributes.toString().split(";"); @@ -258,11 +264,22 @@ public class DecimalFormat extends NumberFormat { as.addAttribute(attribute, attribute, Integer.parseInt(attrs[3*i+1]), Integer.parseInt(attrs[3*i+2])); } - + // return the CharacterIterator from AttributedString return as.getIterator(); } + private int makeScalePositive(int scale, StringBuilder val) { + if (scale < 0) { + scale = -scale; + for (int i = scale; i > 0; i--) { + val.append('0'); + } + scale = 0; + } + return scale; + } + public String toLocalizedPattern() { return NativeDecimalFormat.toPatternImpl(this.addr, true); } @@ -275,30 +292,30 @@ public class DecimalFormat extends NumberFormat { public Number parse(String string, ParsePosition position) { return NativeDecimalFormat.parse(addr, string, position); } - + // start getter and setter @Override public int getMaximumFractionDigits() { - return NativeDecimalFormat.getAttribute(this .addr, + return NativeDecimalFormat.getAttribute(this .addr, UNumberFormatAttribute.UNUM_MAX_FRACTION_DIGITS.ordinal()); } @Override public int getMaximumIntegerDigits() { - return NativeDecimalFormat.getAttribute(this .addr, + return NativeDecimalFormat.getAttribute(this .addr, UNumberFormatAttribute.UNUM_MAX_INTEGER_DIGITS.ordinal()); } @Override public int getMinimumFractionDigits() { - return NativeDecimalFormat.getAttribute(this .addr, + return NativeDecimalFormat.getAttribute(this .addr, UNumberFormatAttribute.UNUM_MIN_FRACTION_DIGITS.ordinal()); } @Override public int getMinimumIntegerDigits() { - return NativeDecimalFormat.getAttribute(this .addr, + return NativeDecimalFormat.getAttribute(this .addr, UNumberFormatAttribute.UNUM_MIN_INTEGER_DIGITS.ordinal()); } @@ -308,12 +325,12 @@ public class DecimalFormat extends NumberFormat { } public int getGroupingSize() { - return NativeDecimalFormat.getAttribute(this.addr, + return NativeDecimalFormat.getAttribute(this.addr, UNumberFormatAttribute.UNUM_GROUPING_SIZE.ordinal()); } public int getMultiplier() { - return NativeDecimalFormat.getAttribute(this.addr, + return NativeDecimalFormat.getAttribute(this.addr, UNumberFormatAttribute.UNUM_MULTIPLIER.ordinal()); } @@ -321,7 +338,7 @@ public class DecimalFormat extends NumberFormat { if (negPrefNull) { return null; } - return NativeDecimalFormat.getTextAttribute(this.addr, + return NativeDecimalFormat.getTextAttribute(this.addr, UNumberFormatTextAttribute.UNUM_NEGATIVE_PREFIX.ordinal()); } @@ -329,7 +346,7 @@ public class DecimalFormat extends NumberFormat { if (negSuffNull) { return null; } - return NativeDecimalFormat.getTextAttribute(this.addr, + return NativeDecimalFormat.getTextAttribute(this.addr, UNumberFormatTextAttribute.UNUM_NEGATIVE_SUFFIX.ordinal()); } @@ -337,7 +354,7 @@ public class DecimalFormat extends NumberFormat { if (posPrefNull) { return null; } - return NativeDecimalFormat.getTextAttribute(this.addr, + return NativeDecimalFormat.getTextAttribute(this.addr, UNumberFormatTextAttribute.UNUM_POSITIVE_PREFIX.ordinal()); } @@ -345,24 +362,24 @@ public class DecimalFormat extends NumberFormat { if (posSuffNull) { return null; } - return NativeDecimalFormat.getTextAttribute(this.addr, + return NativeDecimalFormat.getTextAttribute(this.addr, UNumberFormatTextAttribute.UNUM_POSITIVE_SUFFIX.ordinal()); } public boolean isDecimalSeparatorAlwaysShown() { - return NativeDecimalFormat.getAttribute(this.addr, + return NativeDecimalFormat.getAttribute(this.addr, UNumberFormatAttribute.UNUM_DECIMAL_ALWAYS_SHOWN.ordinal()) != 0; } @Override public boolean isParseIntegerOnly() { - return NativeDecimalFormat.getAttribute(this.addr, + return NativeDecimalFormat.getAttribute(this.addr, UNumberFormatAttribute.UNUM_PARSE_INT_ONLY.ordinal()) != 0; } @Override public boolean isGroupingUsed() { - return NativeDecimalFormat.getAttribute(this.addr, + return NativeDecimalFormat.getAttribute(this.addr, UNumberFormatAttribute.UNUM_GROUPING_USED.ordinal()) != 0; } @@ -376,7 +393,7 @@ public class DecimalFormat extends NumberFormat { public void setDecimalSeparatorAlwaysShown(boolean value) { int i = value ? -1 : 0; - NativeDecimalFormat.setAttribute(this.addr, + NativeDecimalFormat.setAttribute(this.addr, UNumberFormatAttribute.UNUM_DECIMAL_ALWAYS_SHOWN.ordinal(), i); } @@ -465,10 +482,10 @@ public class DecimalFormat extends NumberFormat { @Override public void setParseIntegerOnly(boolean value) { int i = value ? -1 : 0; - NativeDecimalFormat.setAttribute(this.addr, + NativeDecimalFormat.setAttribute(this.addr, UNumberFormatAttribute.UNUM_PARSE_INT_ONLY.ordinal(), i); } - + static protected String getFieldType(Format.Field field) { if(field == null) { return null; @@ -508,7 +525,7 @@ public class DecimalFormat extends NumberFormat { } return null; } - + protected Format.Field getField(String type) { if(type.equals("")) { return null; |