FileDocCategorySizeDatePackage
DecimalFormat.javaAPI DocAndroid 1.5 API19634Wed May 06 22:41:04 BST 2009com.ibm.icu4jni.text

DecimalFormat

public class DecimalFormat extends NumberFormat

Fields Summary
private int
addr
private DecimalFormatSymbols
symbols
private boolean
useExponentialNotation
private byte
minExponentDigits
private boolean
negPrefNull
private boolean
negSuffNull
private boolean
posPrefNull
private boolean
posSuffNull
Constructors Summary
public DecimalFormat(String pattern, DecimalFormatSymbols icuSymbols)


         
        this.addr = icuSymbols.getAddr();
        this.symbols = icuSymbols;
        applyPattern(pattern);
    
Methods Summary
public voidapplyLocalizedPattern(java.lang.String pattern)

        if (pattern == null) {
            throw new NullPointerException("pattern was null");
        }
        try {
            NativeDecimalFormat.applyPatternImpl(this.addr, false, pattern);
        } catch(RuntimeException re) {
            throw new IllegalArgumentException(
                    "applying localized pattern failed for pattern: " + pattern, re);
        }
    
public voidapplyPattern(java.lang.String pattern)

        if (pattern == null) {
            throw new NullPointerException("pattern was null");
        }
        try {
            NativeDecimalFormat.applyPatternImpl(this.addr, false, pattern);
        } catch(RuntimeException re) {
            throw new IllegalArgumentException(
                    "applying pattern failed for pattern: " + pattern, re);
        }
    
public java.lang.Objectclone()

        String pat = this.toPattern();
        Locale loc = this.symbols.getLocale();
        DecimalFormatSymbols sym = (DecimalFormatSymbols) this.symbols.clone();
        DecimalFormat newdf = new DecimalFormat(pat, sym);
        newdf.setMaximumIntegerDigits(this.getMaximumIntegerDigits());
        newdf.setMaximumFractionDigits(this.getMaximumFractionDigits());
        newdf.setMinimumIntegerDigits(this.getMinimumIntegerDigits());
        newdf.setMinimumFractionDigits(this.getMinimumFractionDigits());
        newdf.setGroupingUsed(this.isGroupingUsed());
        newdf.setGroupingSize(this.getGroupingSize());
        return newdf;
    
public booleanequals(java.lang.Object object)

        if (object == this) {
            return true;
        }
        if (!(object instanceof DecimalFormat)) {
            return false;
        }
        DecimalFormat obj = (DecimalFormat) object;

        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();
        result &= obj.getMultiplier() == this.getMultiplier();
        result &= obj.getNegativePrefix().equals(this.getNegativePrefix());
        result &= obj.getNegativeSuffix().equals(this.getNegativeSuffix());
        result &= obj.getPositivePrefix().equals(this.getPositivePrefix());
        result &= obj.getPositiveSuffix().equals(this.getPositiveSuffix());
        result &= obj.getMaximumIntegerDigits() == this.getMaximumIntegerDigits();
        result &= obj.getMaximumFractionDigits() == this.getMaximumFractionDigits();
        result &= obj.getMinimumIntegerDigits() == this.getMinimumIntegerDigits();
        result &= obj.getMinimumFractionDigits() == this.getMinimumFractionDigits();
        result &= obj.isGroupingUsed() == this.isGroupingUsed();
        Currency objCurr = obj.getCurrency();
        Currency thisCurr = this.getCurrency();
        if(objCurr != null) {
            result &= objCurr.getCurrencyCode().equals(thisCurr.getCurrencyCode());
            result &= objCurr.getSymbol().equals(thisCurr.getSymbol());
            result &= objCurr.getDefaultFractionDigits() == thisCurr.getDefaultFractionDigits();
        } else {
            result &= thisCurr == null;
        }
        result &= obj.getDecimalFormatSymbols().equals(this.getDecimalFormatSymbols());

        return result;
    
public java.lang.StringBufferformat(java.lang.Object value, java.lang.StringBuffer buffer, java.text.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,
                    valBigInteger.toString(10), field, fieldType, null, 0);
            return buffer.append(result);
        } else if(number instanceof BigDecimal) {
            BigDecimal valBigDecimal = (BigDecimal) number;
            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();
            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);
            return buffer.append(result);
        }
    
public java.lang.StringBufferformat(long value, java.lang.StringBuffer buffer, java.text.FieldPosition field)


        if(buffer == null || field == null) {
            throw new NullPointerException();
        }

        String fieldType = getFieldType(field.getFieldAttribute());

        String result = NativeDecimalFormat.format(this.addr, value, field,
                fieldType, null);

        buffer.append(result.toCharArray(), 0, result.length());

        return buffer;
    
public java.lang.StringBufferformat(double value, java.lang.StringBuffer buffer, java.text.FieldPosition field)


        if(buffer == null || field == null) {
            throw new NullPointerException();
        }

        String fieldType = getFieldType(field.getFieldAttribute());

        String result = NativeDecimalFormat.format(this.addr, value, field,
                fieldType, null);

        buffer.append(result.toCharArray(), 0, result.length());

        return buffer;
    
public java.text.AttributedCharacterIteratorformatToCharacterIterator(java.lang.Object object)

        if (!(object instanceof Number)) {
            throw new IllegalArgumentException();
        }
        Number number = (Number) object;
        String text = null;
        StringBuffer attributes = new StringBuffer();

        if(number instanceof BigInteger) {
            BigInteger valBigInteger = (BigInteger) number;
            text = NativeDecimalFormat.format(this.addr,
                    valBigInteger.toString(10), null, null, attributes, 0);
        } else if(number instanceof BigDecimal) {
            BigDecimal valBigDecimal = (BigDecimal) number;
            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();
            if (dv == lv) {
                text = NativeDecimalFormat.format(this.addr, lv, null,
                        null, attributes);
            } else {
                text = NativeDecimalFormat.format(this.addr, dv, null,
                        null, attributes);
            }
        }

        AttributedString as = new AttributedString(text.toString());

        String[] attrs = attributes.toString().split(";");
        // add NumberFormat field attributes to the AttributedString
        int size = attrs.length / 3;
        if(size * 3 != attrs.length) {
            return as.getIterator();
        }
        for (int i = 0; i < size; i++) {
            Format.Field attribute = getField(attrs[3*i]);
            as.addAttribute(attribute, attribute, Integer.parseInt(attrs[3*i+1]),
                    Integer.parseInt(attrs[3*i+2]));
        }

        // return the CharacterIterator from AttributedString
        return as.getIterator();
    
public java.util.CurrencygetCurrency()

        return this.symbols.getCurrency();
    
public DecimalFormatSymbolsgetDecimalFormatSymbols()

        return this.symbols;
    
protected java.text.Format$FieldgetField(java.lang.String type)

        if(type.equals("")) {
            return null;
        }
        if(type.equals("sign")) {
            return NumberFormat.Field.SIGN;
        }
        if(type.equals("integer")) {
            return NumberFormat.Field.INTEGER;
        }
        if(type.equals("fraction")) {
            return NumberFormat.Field.FRACTION;
        }
        if(type.equals("exponent")) {
            return NumberFormat.Field.EXPONENT;
        }
        if(type.equals("exponent_sign")) {
            return NumberFormat.Field.EXPONENT_SIGN;
        }
        if(type.equals("exponent_symbol")) {
            return NumberFormat.Field.EXPONENT_SYMBOL;
        }
        if(type.equals("currency")) {
            return NumberFormat.Field.CURRENCY;
        }
        if(type.equals("grouping_separator")) {
            return NumberFormat.Field.GROUPING_SEPARATOR;
        }
        if(type.equals("decimal_separator")) {
            return NumberFormat.Field.DECIMAL_SEPARATOR;
        }
        if(type.equals("percent")) {
            return NumberFormat.Field.PERCENT;
        }
        if(type.equals("permille")) {
            return NumberFormat.Field.PERMILLE;
        }
        return null;
    
protected static java.lang.StringgetFieldType(java.text.Format$Field field)

        if(field == null) {
            return null;
        }
        if(field.equals(NumberFormat.Field.SIGN)) {
            return "sign";
        }
        if(field.equals(NumberFormat.Field.INTEGER)) {
            return "integer";
        }
        if(field.equals(NumberFormat.Field.FRACTION)) {
            return "fraction";
        }
        if(field.equals(NumberFormat.Field.EXPONENT)) {
            return "exponent";
        }
        if(field.equals(NumberFormat.Field.EXPONENT_SIGN)) {
            return "exponent_sign";
        }
        if(field.equals(NumberFormat.Field.EXPONENT_SYMBOL)) {
            return "exponent_symbol";
        }
        if(field.equals(NumberFormat.Field.CURRENCY)) {
            return "currency";
        }
        if(field.equals(NumberFormat.Field.GROUPING_SEPARATOR)) {
            return "grouping_separator";
        }
        if(field.equals(NumberFormat.Field.DECIMAL_SEPARATOR)) {
            return "decimal_separator";
        }
        if(field.equals(NumberFormat.Field.PERCENT)) {
            return "percent";
        }
        if(field.equals(NumberFormat.Field.PERMILLE)) {
            return "permille";
        }
        return null;
    
public intgetGroupingSize()

        return NativeDecimalFormat.getAttribute(this.addr,
                UNumberFormatAttribute.UNUM_GROUPING_SIZE.ordinal());
    
public intgetMaximumFractionDigits()

        return NativeDecimalFormat.getAttribute(this .addr,
                UNumberFormatAttribute.UNUM_MAX_FRACTION_DIGITS.ordinal());
    
public intgetMaximumIntegerDigits()

        return NativeDecimalFormat.getAttribute(this .addr,
                UNumberFormatAttribute.UNUM_MAX_INTEGER_DIGITS.ordinal());
    
public intgetMinimumFractionDigits()

        return NativeDecimalFormat.getAttribute(this .addr,
                UNumberFormatAttribute.UNUM_MIN_FRACTION_DIGITS.ordinal());
    
public intgetMinimumIntegerDigits()

        return NativeDecimalFormat.getAttribute(this .addr,
                UNumberFormatAttribute.UNUM_MIN_INTEGER_DIGITS.ordinal());
    
public intgetMultiplier()

        return NativeDecimalFormat.getAttribute(this.addr,
                UNumberFormatAttribute.UNUM_MULTIPLIER.ordinal());
    
public java.lang.StringgetNegativePrefix()

        if (negPrefNull) {
            return null;
        }
        return NativeDecimalFormat.getTextAttribute(this.addr,
                UNumberFormatTextAttribute.UNUM_NEGATIVE_PREFIX.ordinal());
    
public java.lang.StringgetNegativeSuffix()

        if (negSuffNull) {
            return null;
        }
        return NativeDecimalFormat.getTextAttribute(this.addr,
                UNumberFormatTextAttribute.UNUM_NEGATIVE_SUFFIX.ordinal());
    
public java.lang.StringgetPositivePrefix()

        if (posPrefNull) {
            return null;
        }
        return NativeDecimalFormat.getTextAttribute(this.addr,
                UNumberFormatTextAttribute.UNUM_POSITIVE_PREFIX.ordinal());
    
public java.lang.StringgetPositiveSuffix()

        if (posSuffNull) {
            return null;
        }
        return NativeDecimalFormat.getTextAttribute(this.addr,
                UNumberFormatTextAttribute.UNUM_POSITIVE_SUFFIX.ordinal());
    
public inthashCode()

        return super.hashCode() * 37 + this.getPositivePrefix().hashCode();
    
public booleanisDecimalSeparatorAlwaysShown()

        return NativeDecimalFormat.getAttribute(this.addr,
                UNumberFormatAttribute.UNUM_DECIMAL_ALWAYS_SHOWN.ordinal()) != 0;
    
public booleanisGroupingUsed()

        return NativeDecimalFormat.getAttribute(this.addr,
                UNumberFormatAttribute.UNUM_GROUPING_USED.ordinal()) != 0;
    
public booleanisParseIntegerOnly()

        return NativeDecimalFormat.getAttribute(this.addr,
                UNumberFormatAttribute.UNUM_PARSE_INT_ONLY.ordinal()) != 0;
    
private intmakeScalePositive(int scale, java.lang.StringBuilder val)

        if (scale < 0) {
            scale = -scale;
            for (int i = scale; i > 0; i--) {
                val.append('0");
            }
            scale = 0;
        }
        return scale;
    
public java.lang.Numberparse(java.lang.String string, java.text.ParsePosition position)

        return NativeDecimalFormat.parse(addr, string, position);
    
public voidsetCurrency(java.util.Currency currency)

        this.symbols.setCurrency(currency);
    
public voidsetDecimalFormatSymbols(DecimalFormatSymbols icuSymbols)

        this.symbols = icuSymbols;
    
public voidsetDecimalSeparatorAlwaysShown(boolean value)

        int i = value ? -1 : 0;
        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_DECIMAL_ALWAYS_SHOWN.ordinal(), i);
    
public voidsetGroupingSize(int value)

        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_GROUPING_SIZE.ordinal(), value);
    
public voidsetGroupingUsed(boolean value)

        int i = value ? -1 : 0;
        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_GROUPING_USED.ordinal(), i);
    
public voidsetMaximumFractionDigits(int value)

        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_MAX_FRACTION_DIGITS.ordinal(), value);
    
public voidsetMaximumIntegerDigits(int value)

        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_MAX_INTEGER_DIGITS.ordinal(), value);
    
public voidsetMinimumFractionDigits(int value)

        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_MIN_FRACTION_DIGITS.ordinal(), value);
    
public voidsetMinimumIntegerDigits(int value)

        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_MIN_INTEGER_DIGITS.ordinal(), value);
    
public voidsetMultiplier(int value)

        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_MULTIPLIER.ordinal(), value);
    
public voidsetNegativePrefix(java.lang.String value)

        negPrefNull = value == null;
        if (!negPrefNull) {
            NativeDecimalFormat.setTextAttribute(this.addr,
                    UNumberFormatTextAttribute.UNUM_NEGATIVE_PREFIX.ordinal(),
                    value);
        }
    
public voidsetNegativeSuffix(java.lang.String value)

        negSuffNull = value == null;
        if (!negSuffNull) {
            NativeDecimalFormat.setTextAttribute(this.addr,
                    UNumberFormatTextAttribute.UNUM_NEGATIVE_SUFFIX.ordinal(),
                    value);
        }
    
public voidsetParseIntegerOnly(boolean value)

        int i = value ? -1 : 0;
        NativeDecimalFormat.setAttribute(this.addr,
                UNumberFormatAttribute.UNUM_PARSE_INT_ONLY.ordinal(), i);
    
public voidsetPositivePrefix(java.lang.String value)

        posPrefNull = value == null;
        if (!posPrefNull) {
            NativeDecimalFormat.setTextAttribute(this.addr,
                    UNumberFormatTextAttribute.UNUM_POSITIVE_PREFIX.ordinal(),
                    value);
        }
    
public voidsetPositiveSuffix(java.lang.String value)

        posSuffNull = value == null;
        if (!posSuffNull) {
            NativeDecimalFormat.setTextAttribute(this.addr,
                    UNumberFormatTextAttribute.UNUM_POSITIVE_SUFFIX.ordinal(),
                    value);
        }
    
public java.lang.StringtoLocalizedPattern()

        return NativeDecimalFormat.toPatternImpl(this.addr, true);
    
public java.lang.StringtoPattern()

        return NativeDecimalFormat.toPatternImpl(this.addr, false);