FileDocCategorySizeDatePackage
DecimalFormatSymbols.javaAPI DocJava SE 5 API19949Fri Aug 26 14:57:20 BST 2005java.text

DecimalFormatSymbols

public final class DecimalFormatSymbols extends Object implements Serializable, Cloneable
This class represents the set of symbols (such as the decimal separator, the grouping separator, and so on) needed by DecimalFormat to format numbers. DecimalFormat creates for itself an instance of DecimalFormatSymbols from its locale data. If you need to change any of these symbols, you can get the DecimalFormatSymbols object from your DecimalFormat and modify it.
see
java.util.Locale
see
DecimalFormat
version
1.41, 05/10/04
author
Mark Davis
author
Alan Liu

Fields Summary
private char
zeroDigit
Character used for zero.
private char
groupingSeparator
Character used for thousands separator.
private char
decimalSeparator
Character used for decimal sign.
private char
perMill
Character used for per mille sign.
private char
percent
Character used for percent sign.
private char
digit
Character used for a digit in a pattern.
private char
patternSeparator
Character used to separate positive and negative subpatterns in a pattern.
private String
infinity
String used to represent infinity.
private String
NaN
String used to represent "not a number".
private char
minusSign
Character used to represent minus sign.
private String
currencySymbol
String denoting the local currency, e.g. "$".
private String
intlCurrencySymbol
ISO 4217 currency code denoting the local currency, e.g. "USD".
private char
monetarySeparator
The decimal separator used when formatting currency values.
private char
exponential
The character used to distinguish the exponent in a number formatted in exponential notation, e.g. 'E' for a number such as "1.23E45".

Note that the public API provides no way to set this field, even though it is supported by the implementation and the stream format. The intent is that this will be added to the API in the future.

private Locale
locale
The locale of these currency format symbols.
private transient Currency
currency
static final long
serialVersionUID
private static final int
currentSerialVersion
private int
serialVersionOnStream
Describes the version of DecimalFormatSymbols present on the stream. Possible values are:
  • 0 (or uninitialized): versions prior to JDK 1.1.6.
  • 1: Versions written by JDK 1.1.6 or later, which include two new fields: monetarySeparator and exponential.
  • 2: Versions written by J2SE 1.4 or later, which include a new locale field.
When streaming out a DecimalFormatSymbols, the most recent format (corresponding to the highest allowable serialVersionOnStream) is always written.
private static final Hashtable
cachedLocaleData
cache to hold the NumberElements and the Currency of a Locale.
Constructors Summary
public DecimalFormatSymbols()
Create a DecimalFormatSymbols object for the default locale.

        initialize( Locale.getDefault() );
    
public DecimalFormatSymbols(Locale locale)
Create a DecimalFormatSymbols object for the given locale.

exception
NullPointerException if locale is null

        initialize( locale );
    
Methods Summary
public java.lang.Objectclone()
Standard override.

        try {
            return (DecimalFormatSymbols)super.clone();
            // other fields are bit-copied
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    
public booleanequals(java.lang.Object obj)
Override equals.

        if (obj == null) return false;
        if (this == obj) return true;
        if (getClass() != obj.getClass()) return false;
        DecimalFormatSymbols other = (DecimalFormatSymbols) obj;
        return (zeroDigit == other.zeroDigit &&
        groupingSeparator == other.groupingSeparator &&
        decimalSeparator == other.decimalSeparator &&
        percent == other.percent &&
        perMill == other.perMill &&
        digit == other.digit &&
        minusSign == other.minusSign &&
        patternSeparator == other.patternSeparator &&
        infinity.equals(other.infinity) &&
        NaN.equals(other.NaN) &&
        currencySymbol.equals(other.currencySymbol) &&
        intlCurrencySymbol.equals(other.intlCurrencySymbol) &&
        currency == other.currency &&
        monetarySeparator == other.monetarySeparator &&
        locale.equals(other.locale));
    
public java.util.CurrencygetCurrency()
Gets the currency of these DecimalFormatSymbols. May be null if the currency symbol attribute was previously set to a value that's not a valid ISO 4217 currency code.

return
the currency used, or null
since
1.4

        return currency;
    
public java.lang.StringgetCurrencySymbol()
Returns the currency symbol for the currency of these DecimalFormatSymbols in their locale.

since
1.2

        return currencySymbol;
    
public chargetDecimalSeparator()
Gets the character used for decimal sign. Different for French, etc.

        return decimalSeparator;
    
public chargetDigit()
Gets the character used for a digit in a pattern.

        return digit;
    
chargetExponentialSymbol()
Returns the character used to separate the mantissa from the exponent.

        return exponential;
    
public chargetGroupingSeparator()
Gets the character used for thousands separator. Different for French, etc.

        return groupingSeparator;
    
public java.lang.StringgetInfinity()
Gets the string used to represent infinity. Almost always left unchanged.

        return infinity;
    
public java.lang.StringgetInternationalCurrencySymbol()
Returns the ISO 4217 currency code of the currency of these DecimalFormatSymbols.

since
1.2

        return intlCurrencySymbol;
    
public chargetMinusSign()
Gets the character used to represent minus sign. If no explicit negative format is specified, one is formed by prefixing minusSign to the positive format.

        return minusSign;
    
public chargetMonetaryDecimalSeparator()
Returns the monetary decimal separator.

since
1.2

        return monetarySeparator;
    
public java.lang.StringgetNaN()
Gets the string used to represent "not a number". Almost always left unchanged.

        return NaN;
    
public chargetPatternSeparator()
Gets the character used to separate positive and negative subpatterns in a pattern.

        return patternSeparator;
    
public chargetPerMill()
Gets the character used for per mille sign. Different for Arabic, etc.

        return perMill;
    
public chargetPercent()
Gets the character used for percent sign. Different for Arabic, etc.

        return percent;
    
public chargetZeroDigit()
Gets the character used for zero. Different for Arabic, etc.

        return zeroDigit;
    
public inthashCode()
Override hashCode.

            int result = zeroDigit;
            result = result * 37 + groupingSeparator;
            result = result * 37 + decimalSeparator;
            return result;
    
private voidinitialize(java.util.Locale locale)
Initializes the symbols from the LocaleElements resource bundle.

        this.locale = locale;

        // get resource bundle data - try the cache first
        boolean needCacheUpdate = false;
        Object[] data = (Object[]) cachedLocaleData.get(locale);
        if (data == null) {  /* cache miss */
            data = new Object[3];
            ResourceBundle rb = LocaleData.getLocaleElements(locale);
            data[0] = rb.getStringArray("NumberElements");
            needCacheUpdate = true;
        }

        String[] numberElements = (String[]) data[0];;

        decimalSeparator = numberElements[0].charAt(0);
        groupingSeparator = numberElements[1].charAt(0);
        patternSeparator = numberElements[2].charAt(0);
        percent = numberElements[3].charAt(0);
        zeroDigit = numberElements[4].charAt(0); //different for Arabic,etc.
        digit = numberElements[5].charAt(0);
        minusSign = numberElements[6].charAt(0);
        exponential = numberElements[7].charAt(0);
        perMill = numberElements[8].charAt(0);
        infinity  = numberElements[9];
        NaN = numberElements[10];
        
        // Try to obtain the currency used in the locale's country.
        // Check for empty country string separately because it's a valid
        // country ID for Locale (and used for the C locale), but not a valid
        // ISO 3166 country code, and exceptions are expensive.
        if (!"".equals(locale.getCountry())) {
            try {
                currency = Currency.getInstance(locale);
            } catch (IllegalArgumentException e) {
                // use default values below for compatibility
            }
        }
        if (currency != null) {
            intlCurrencySymbol = currency.getCurrencyCode();
            if (data[1] != null && data[1] == intlCurrencySymbol) {
                currencySymbol = (String) data[2];
            } else {
                currencySymbol = currency.getSymbol(locale);
                data[1] = intlCurrencySymbol;
                data[2] = currencySymbol;
                needCacheUpdate = true;
            }
        } else {
            // default values
            intlCurrencySymbol = "XXX";
            try {
                currency = Currency.getInstance(intlCurrencySymbol);
            } catch (IllegalArgumentException e) {
            }
            currencySymbol = "\u00A4";
        }
        // Currently the monetary decimal separator is the same as the
        // standard decimal separator for all locales that we support.
        // If that changes, add a new entry to NumberElements.
        monetarySeparator = decimalSeparator;
        
        if (needCacheUpdate) {
            cachedLocaleData.put(locale, data);
        }
    
private voidreadObject(java.io.ObjectInputStream stream)
Reads the default serializable fields, provides default values for objects in older serial versions, and initializes non-serializable fields. If serialVersionOnStream is less than 1, initializes monetarySeparator to be the same as decimalSeparator and exponential to be 'E'. If serialVersionOnStream is less then 2, initializes localeto the root locale. Sets serialVersionOnStream back to the maximum allowed value so that default serialization will work properly if this object is streamed out again. Initializes the currency from the intlCurrencySymbol field.

since
JDK 1.1.6

        stream.defaultReadObject();
        if (serialVersionOnStream < 1) {
            // Didn't have monetarySeparator or exponential field;
            // use defaults.
            monetarySeparator = decimalSeparator;
            exponential       = 'E";
        }
        if (serialVersionOnStream < 2) {
            // didn't have locale; use root locale
            locale = new Locale("");
        }
        serialVersionOnStream = currentSerialVersion;

        if (intlCurrencySymbol != null) {
            try {
                 currency = Currency.getInstance(intlCurrencySymbol);
            } catch (IllegalArgumentException e) {
            }
        }
    
public voidsetCurrency(java.util.Currency currency)
Sets the currency of these DecimalFormatSymbols. This also sets the currency symbol attribute to the currency's symbol in the DecimalFormatSymbols' locale, and the international currency symbol attribute to the currency's ISO 4217 currency code.

param
currency the new currency to be used
exception
NullPointerException if currency is null
since
1.4
see
#setCurrencySymbol
see
#setInternationalCurrencySymbol

        if (currency == null) {
            throw new NullPointerException();
        }
        this.currency = currency;
        intlCurrencySymbol = currency.getCurrencyCode();
        currencySymbol = currency.getSymbol(locale);
    
public voidsetCurrencySymbol(java.lang.String currency)
Sets the currency symbol for the currency of these DecimalFormatSymbols in their locale.

since
1.2

        currencySymbol = currency;
    
public voidsetDecimalSeparator(char decimalSeparator)
Sets the character used for decimal sign. Different for French, etc.

        this.decimalSeparator = decimalSeparator;
    
public voidsetDigit(char digit)
Sets the character used for a digit in a pattern.

        this.digit = digit;
    
voidsetExponentialSymbol(char exp)
Sets the character used to separate the mantissa from the exponent.

        exponential = exp;
    
public voidsetGroupingSeparator(char groupingSeparator)
Sets the character used for thousands separator. Different for French, etc.

        this.groupingSeparator = groupingSeparator;
    
public voidsetInfinity(java.lang.String infinity)
Sets the string used to represent infinity. Almost always left unchanged.

        this.infinity = infinity;
    
public voidsetInternationalCurrencySymbol(java.lang.String currencyCode)
Sets the ISO 4217 currency code of the currency of these DecimalFormatSymbols. If the currency code is valid (as defined by {@link java.util.Currency#getInstance(java.lang.String) Currency.getInstance}), this also sets the currency attribute to the corresponding Currency instance and the currency symbol attribute to the currency's symbol in the DecimalFormatSymbols' locale. If the currency code is not valid, then the currency attribute is set to null and the currency symbol attribute is not modified.

see
#setCurrency
see
#setCurrencySymbol
since
1.2

        intlCurrencySymbol = currencyCode;
        currency = null;
        if (currencyCode != null) {
            try {
                currency = Currency.getInstance(currencyCode);
                currencySymbol = currency.getSymbol();
            } catch (IllegalArgumentException e) {
            }
        }
    
public voidsetMinusSign(char minusSign)
Sets the character used to represent minus sign. If no explicit negative format is specified, one is formed by prefixing minusSign to the positive format.

        this.minusSign = minusSign;
    
public voidsetMonetaryDecimalSeparator(char sep)
Sets the monetary decimal separator.

since
1.2

        monetarySeparator = sep;
    
public voidsetNaN(java.lang.String NaN)
Sets the string used to represent "not a number". Almost always left unchanged.

        this.NaN = NaN;
    
public voidsetPatternSeparator(char patternSeparator)
Sets the character used to separate positive and negative subpatterns in a pattern.

        this.patternSeparator = patternSeparator;
    
public voidsetPerMill(char perMill)
Sets the character used for per mille sign. Different for Arabic, etc.

        this.perMill = perMill;
    
public voidsetPercent(char percent)
Sets the character used for percent sign. Different for Arabic, etc.

        this.percent = percent;
    
public voidsetZeroDigit(char zeroDigit)
Sets the character used for zero. Different for Arabic, etc.

        this.zeroDigit = zeroDigit;