Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
For a Java application that drags data together from some sources and does some calculation itself, we want to offer users the possibility to use their own format strings, and would prefer the format string syntax they know from Excel (e. g. "$ "#,###.,0) which happens to be the same used in .net and Analysis Services. The closest thing to use that I found in Java is DecimalFormat which lacks ...
Given a locale java.text.NumberFormat: NumberFormat numberFormat = NumberFormat.getInstance(); How can I get the character used as Decimal separator (if it's a comma or a point) in that numberformat? How can I modify this property without having to use new DecimalFormat(format)? Thanks
Ok, so here it is. I have this little crazy method that converts BigDecimal values into nice and readable Strings. private String formatBigDecimal(BigDecimal bd){ DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(3); df.setMaximumFractionDigits(3); df.setMinimumIntegerDigits(1); df.setMaximumIntegerDigits(3); df.setGroupingSize(20); return df.forma...
Would be grateful your help. What would be elegant Regx to check a String whether it holds a number ? any suggestions? thanks.
I need to edit format for JFormatedTextField in a Java program. NetBeans are "helping" me with something called Format editor. But, I have no clue how the pattern works. For #,##0.### , it returns 1,234.567, as pictured above. However, I want to change the thousands delimiter to space and decimal separator to comma. I would guess # ##0,### is the right format, but no, that returns "Malformed...
decimal seperator is a dot, followed by max one digit! No range specified. Thanks guys!
I'm working with NumberField and NumberFormat. How can I change NumberConstants.decimalSeparator() value without changing my locale? For instance I want to have "." or "," as decimal separators for all locales. Is it possible to create custom property file NumberConstants_bla.properties and force gxt use it instead of standard NumberConstants_ru_RU.properties or NumberConstants_en_US.properties...
I never know how to document a system in a way that other developers will find it easy to use and/or maintain it. Documentating the source code is good and required for that, but I don't think it is enough to give a good overview of the application itself. Java seems to keep most of its documentation in the source code, which describes only basic usage of certain classes - creating a DecimalFo...
  /*
   * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 /*
  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  *
  *   The original version of this source code and documentation is copyrighted
  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  * materials are provided under terms of a License Agreement between Taligent
  * and Sun. This technology is protected by multiple US and International
  * patents. This notice and attribution to Taligent may not be removed.
  *   Taligent is a registered trademark of Taligent, Inc.
  *
  */
 
 package java.text;
 
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.

Author(s):
Mark Davis
Alan Liu
See also:
java.util.Locale
DecimalFormat
 
 
 public class DecimalFormatSymbols implements CloneableSerializable {

    
Create a DecimalFormatSymbols object for the default locale. This constructor can only construct instances for the locales supported by the Java runtime environment, not for those supported by installed DecimalFormatSymbolsProvider implementations. For full locale coverage, use the getInstance method.
 
     public DecimalFormatSymbols() {
         initialize( Locale.getDefault() );
     }

    
Create a DecimalFormatSymbols object for the given locale. This constructor can only construct instances for the locales supported by the Java runtime environment, not for those supported by installed DecimalFormatSymbolsProvider implementations. For full locale coverage, use the getInstance method.

Throws:
java.lang.NullPointerException if locale is null
 
     public DecimalFormatSymbolsLocale locale ) {
         initializelocale );
     }

    
Returns an array of all locales for which the getInstance methods of this class can return localized instances. The returned array represents the union of locales supported by the Java runtime and by installed DecimalFormatSymbolsProvider implementations. It must contain at least a Locale instance equal to Locale.US.

Returns:
An array of locales for which localized DecimalFormatSymbols instances are available.
Since:
1.6
    public static Locale[] getAvailableLocales() {
        LocaleServiceProviderPool pool =
            LocaleServiceProviderPool.getPool(DecimalFormatSymbolsProvider.class);
        return pool.getAvailableLocales();
    }

    
Gets the DecimalFormatSymbols instance for the default locale. This method provides access to DecimalFormatSymbols instances for locales supported by the Java runtime itself as well as for those supported by installed DecimalFormatSymbolsProvider implementations.

Returns:
a DecimalFormatSymbols instance.
Since:
1.6
    public static final DecimalFormatSymbols getInstance() {
        return getInstance(Locale.getDefault());
    }

    
Gets the DecimalFormatSymbols instance for the specified locale. This method provides access to DecimalFormatSymbols instances for locales supported by the Java runtime itself as well as for those supported by installed DecimalFormatSymbolsProvider implementations.

Parameters:
locale the desired locale.
Returns:
a DecimalFormatSymbols instance.
Throws:
java.lang.NullPointerException if locale is null
Since:
1.6
    public static final DecimalFormatSymbols getInstance(Locale locale) {
        // Check whether a provider can provide an implementation that's closer
        // to the requested locale than what the Java runtime itself can provide.
        LocaleServiceProviderPool pool =
            LocaleServiceProviderPool.getPool(DecimalFormatSymbolsProvider.class);
        if (pool.hasProviders()) {
            DecimalFormatSymbols providersInstance = pool.getLocalizedObject(
                                .locale);
            if (providersInstance != null) {
                return providersInstance;
            }
        }
        return new DecimalFormatSymbols(locale);
    }

    
Gets the character used for zero. Different for Arabic, etc.
    public char getZeroDigit() {
        return ;
    }

    
Sets the character used for zero. Different for Arabic, etc.
    public void setZeroDigit(char zeroDigit) {
        this. = zeroDigit;
    }

    
Gets the character used for thousands separator. Different for French, etc.
    public char getGroupingSeparator() {
        return ;
    }

    
Sets the character used for thousands separator. Different for French, etc.
    public void setGroupingSeparator(char groupingSeparator) {
        this. = groupingSeparator;
    }

    
Gets the character used for decimal sign. Different for French, etc.
    public char getDecimalSeparator() {
        return ;
    }

    
Sets the character used for decimal sign. Different for French, etc.
    public void setDecimalSeparator(char decimalSeparator) {
        this. = decimalSeparator;
    }

    
Gets the character used for per mille sign. Different for Arabic, etc.
    public char getPerMill() {
        return ;
    }

    
Sets the character used for per mille sign. Different for Arabic, etc.
    public void setPerMill(char perMill) {
        this. = perMill;
    }

    
Gets the character used for percent sign. Different for Arabic, etc.
    public char getPercent() {
        return ;
    }

    
Sets the character used for percent sign. Different for Arabic, etc.
    public void setPercent(char percent) {
        this. = percent;
    }

    
Gets the character used for a digit in a pattern.
    public char getDigit() {
        return ;
    }

    
Sets the character used for a digit in a pattern.
    public void setDigit(char digit) {
        this. = digit;
    }

    
Gets the character used to separate positive and negative subpatterns in a pattern.
    public char getPatternSeparator() {
        return ;
    }

    
Sets the character used to separate positive and negative subpatterns in a pattern.
    public void setPatternSeparator(char patternSeparator) {
        this. = patternSeparator;
    }

    
Gets the string used to represent infinity. Almost always left unchanged.
    public String getInfinity() {
        return ;
    }

    
Sets the string used to represent infinity. Almost always left unchanged.
    public void setInfinity(String infinity) {
        this. = infinity;
    }

    
Gets the string used to represent "not a number". Almost always left unchanged.
    public String getNaN() {
        return ;
    }

    
Sets the string used to represent "not a number". Almost always left unchanged.
    public void setNaN(String NaN) {
        this. = NaN;
    }

    
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.
    public char getMinusSign() {
        return ;
    }

    
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.
    public void setMinusSign(char minusSign) {
        this. = minusSign;
    }

    
Returns the currency symbol for the currency of these DecimalFormatSymbols in their locale.

Since:
1.2
    public String getCurrencySymbol()
    {
        return ;
    }

    
Sets the currency symbol for the currency of these DecimalFormatSymbols in their locale.

Since:
1.2
    public void setCurrencySymbol(String currency)
    {
         = currency;
    }

    
Returns the ISO 4217 currency code of the currency of these DecimalFormatSymbols.

Since:
1.2
    {
        return ;
    }

    
Sets the ISO 4217 currency code of the currency of these DecimalFormatSymbols. If the currency code is valid (as defined by 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.

    public void setInternationalCurrencySymbol(String currencyCode)
    {
         = currencyCode;
         = null;
        if (currencyCode != null) {
            try {
                 = Currency.getInstance(currencyCode);
                 = .getSymbol();
            } catch (IllegalArgumentException e) {
            }
        }
    }

    
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.

Returns:
the currency used, or null
Since:
1.4
    public Currency getCurrency() {
        return ;
    }

    
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.

Parameters:
currency the new currency to be used
Throws:
java.lang.NullPointerException if currency is null
Since:
1.4
See also:
setCurrencySymbol(java.lang.String)
setInternationalCurrencySymbol(java.lang.String)
    public void setCurrency(Currency currency) {
        if (currency == null) {
            throw new NullPointerException();
        }
        this. = currency;
         = currency.getCurrencyCode();
         = currency.getSymbol();
    }


    
Returns the monetary decimal separator.

Since:
1.2
    public char getMonetaryDecimalSeparator()
    {
        return ;
    }

    
Sets the monetary decimal separator.

Since:
1.2
    public void setMonetaryDecimalSeparator(char sep)
    {
         = sep;
    }
    //------------------------------------------------------------
    // BEGIN   Package Private methods ... to be made public later
    //------------------------------------------------------------

    
Returns the character used to separate the mantissa from the exponent.
    char getExponentialSymbol()
    {
        return ;
    }
  
Returns the string used to separate the mantissa from the exponent. Examples: "x10^" for 1.23x10^4, "E" for 1.23E4.

Returns:
the exponent separator string
Since:
1.6
See also:
setExponentSeparator(java.lang.String)
    public String getExponentSeparator()
    {
        return ;
    }

    
Sets the character used to separate the mantissa from the exponent.
    void setExponentialSymbol(char exp)
    {
         = exp;
    }

  
Sets the string used to separate the mantissa from the exponent. Examples: "x10^" for 1.23x10^4, "E" for 1.23E4.

Parameters:
exp the exponent separator string
Throws:
java.lang.NullPointerException if exp is null
Since:
1.6
See also:
getExponentSeparator()
    public void setExponentSeparator(String exp)
    {
        if (exp == null) {
            throw new NullPointerException();
        }
         = exp;
     }
    //------------------------------------------------------------
    // END     Package Private methods ... to be made public later
    //------------------------------------------------------------

    
Standard override.
    public Object clone() {
        try {
            return (DecimalFormatSymbols)super.clone();
            // other fields are bit-copied
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }

    
Override equals.
    public boolean equals(Object obj) {
        if (obj == nullreturn false;
        if (this == objreturn true;
        if (getClass() != obj.getClass()) return false;
        DecimalFormatSymbols other = (DecimalFormatSymbolsobj;
        return ( == other.zeroDigit &&
         == other.groupingSeparator &&
         == other.decimalSeparator &&
         == other.percent &&
         == other.perMill &&
         == other.digit &&
         == other.minusSign &&
         == other.patternSeparator &&
        .equals(other.infinity) &&
        .equals(other.NaN) &&
        .equals(other.currencySymbol) &&
        .equals(other.intlCurrencySymbol) &&
         == other.currency &&
         == other.monetarySeparator &&
        .equals(other.exponentialSeparator) &&
        .equals(other.locale));
    }

    
Override hashCode.
    public int hashCode() {
            int result = ;
            result = result * 37 + ;
            result = result * 37 + ;
            return result;
    }

    
Initializes the symbols from the FormatData resource bundle.
    private void initializeLocale locale ) {
        this. = locale;
        // get resource bundle data - try the cache first
        boolean needCacheUpdate = false;
        Object[] data = (Object[]) .get(locale);
        if (data == null) {  /* cache miss */
            data = new Object[3];
            ResourceBundle rb = LocaleData.getNumberFormatData(locale);
            data[0] = rb.getStringArray("NumberElements");
            needCacheUpdate = true;
        }
        String[] numberElements = (String[]) data[0];
         = numberElements[0].charAt(0);
         = numberElements[1].charAt(0);
         = numberElements[2].charAt(0);
         = numberElements[3].charAt(0);
         = numberElements[4].charAt(0); //different for Arabic,etc.
         = numberElements[5].charAt(0);
         = numberElements[6].charAt(0);
         = numberElements[7].charAt(0);
         = numberElements[7]; //string representation new since 1.6
         = numberElements[8].charAt(0);
          = numberElements[9];
         = 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.getInstance(locale);
            } catch (IllegalArgumentException e) {
                // use default values below for compatibility
            }
        }
        if ( != null) {
             = .getCurrencyCode();
            if (data[1] != null && data[1] == ) {
                 = (Stringdata[2];
            } else {
                 = .getSymbol(locale);
                data[1] = ;
                data[2] = ;
                needCacheUpdate = true;
            }
        } else {
            // default values
             = "XXX";
            try {
                 = Currency.getInstance();
            } catch (IllegalArgumentException e) {
            }
             = "\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.
        if (needCacheUpdate) {
            .put(localedata);
        }
    }

    
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 than 2, initializes localeto the root locale, and initializes If serialVersionOnStream is less than 3, it initializes exponentialSeparator using exponential. 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
    private void readObject(ObjectInputStream stream)
            throws IOExceptionClassNotFoundException {
        stream.defaultReadObject();
        if ( < 1) {
            // Didn't have monetarySeparator or exponential field;
            // use defaults.
             = ;
                   = 'E';
        }
        if ( < 2) {
            // didn't have locale; use root locale
             = .;
        }
        if ( < 3) {
            // didn't have exponentialSeparator. Create one using exponential
             = Character.toString();
        }
        if ( != null) {
            try {
                  = Currency.getInstance();
            } catch (IllegalArgumentException e) {
            }
        }
    }

    
Character used for zero.

See also:
getZeroDigit()
Serial:
    private  char    zeroDigit;

    
Character used for thousands separator.

See also:
getGroupingSeparator()
Serial:
    private  char    groupingSeparator;

    
Character used for decimal sign.

See also:
getDecimalSeparator()
Serial:
    private  char    decimalSeparator;

    
Character used for per mille sign.

See also:
getPerMill()
Serial:
    private  char    perMill;

    
Character used for percent sign.

See also:
getPercent()
Serial:
    private  char    percent;

    
Character used for a digit in a pattern.

See also:
getDigit()
Serial:
    private  char    digit;

    
Character used to separate positive and negative subpatterns in a pattern.

See also:
getPatternSeparator()
Serial:
    private  char    patternSeparator;

    
String used to represent infinity.

See also:
getInfinity()
Serial:
    private  String  infinity;

    
String used to represent "not a number".

See also:
getNaN()
Serial:
    private  String  NaN;

    
Character used to represent minus sign.

See also:
getMinusSign()
Serial:
    private  char    minusSign;

    
String denoting the local currency, e.g. "$".

See also:
getCurrencySymbol()
Serial:
    private  String  currencySymbol;

    
ISO 4217 currency code denoting the local currency, e.g. "USD".

    private  String  intlCurrencySymbol;

    
The decimal separator used when formatting currency values.

Since:
JDK 1.1.6
See also:
getMonetaryDecimalSeparator()
Serial:
    private  char    monetarySeparator// Field new in JDK 1.1.6

    
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.

Since:
JDK 1.1.6
Serial:
    private  char    exponential;       // Field new in JDK 1.1.6

  
The string used to separate the mantissa from the exponent. Examples: "x10^" for 1.23x10^4, "E" for 1.23E4.

If both exponential and exponentialSeparator exist, this exponentialSeparator has the precedence.

Since:
1.6
Serial:
    private  String    exponentialSeparator;       // Field new in JDK 1.6

    
The locale of these currency format symbols.

Since:
1.4
Serial:
    private Locale locale;
    // currency; only the ISO code is serialized.
    private transient Currency currency;
    // Proclaim JDK 1.1 FCS compatibility
    static final long serialVersionUID = 5772796243397350300L;
    // The internal serial version which says which version was written
    // - 0 (default) for version up to JDK 1.1.5
    // - 1 for version from JDK 1.1.6, which includes two new fields:
    //     monetarySeparator and exponential.
    // - 2 for version from J2SE 1.4, which includes locale field.
    // - 3 for version from J2SE 1.6, which includes exponentialSeparator field.
    private static final int currentSerialVersion = 3;

    
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.
  • 3: Versions written by J2SE 1.6 or later, which include a new exponentialSeparator field.
When streaming out a DecimalFormatSymbols, the most recent format (corresponding to the highest allowable serialVersionOnStream) is always written.

Since:
JDK 1.1.6
Serial:
    private int serialVersionOnStream = ;

    
cache to hold the NumberElements and the Currency of a Locale.
    private static final Hashtable cachedLocaleData = new Hashtable(3);

    
Obtains a DecimalFormatSymbols instance from a DecimalFormatSymbolsProvider implementation.
    private static class DecimalFormatSymbolsGetter
                                                                   DecimalFormatSymbols> {
        private static final DecimalFormatSymbolsGetter INSTANCE =
            new DecimalFormatSymbolsGetter();
        public DecimalFormatSymbols getObject(
                                DecimalFormatSymbolsProvider decimalFormatSymbolsProvider,
                                Locale locale,
                                String key,
                                Object... params) {
            assert params.length == 0;
            return decimalFormatSymbolsProvider.getInstance(locale);
        }
    }
New to GrepCode? Check out our FAQ X