DecimalFormat Example
Home ] Up ] Using NumberFormat ] Using DecimalFormat ] [ DecimalFormat Example ] Using MessageFormat ]

 

 

Here's an example using DecimalFormat:

 package inputOutput;
 
 import java.util.Locale;
 import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.text.ParseException;
 
 public class FormatDecimal
 {
    public static void main(String[] args)
    {
        // Gather the available locales
        Locale[] locales = NumberFormat.getAvailableLocales();

        double myNumber = -1234.56;
        NumberFormat form;

        // Print out a number with the locale number, currency
        // and percent format for each locale we can.
        for (int j = 0; j < 3; ++j) 
        {
            System.out.println("FORMAT");
            for (int i = 0; i < locales.length; ++i) 
            {
                if (locales[i].getCountry().length() == 0) 
                {
                    // skip language-only
                    continue;
                }
                System.out.print(locales[i].getDisplayName());
                switch (j) 
                {
                  default:
                    form = NumberFormat.getInstance(locales[i]); 
                    break;
                  case 1:
                    form = NumberFormat.getCurrencyInstance(locales[i]); 
                    break;
                  case 0:
                    form = NumberFormat.getPercentInstance(locales[i]); 
                    break;
                }
                try 
                {
                    System.out.print(": " + 
                                     ((DecimalFormat)form).toPattern() +
                                     " -> " + form.format(myNumber));
                } 
                catch (IllegalArgumentException iae) 
                { }
                try 
                {
                    System.out.println(" -> " + 
                                       form.parse(
                                            form.format(myNumber)));
                } 
                catch (ParseException pe) 
                { }
            }
        }
    }
 }

which, on my Microsoft Windows system, prints out a very long list:

FORMAT
Arabic (Egypt): #,##0% -> -123,456% -> -1234.56
Byelorussian (Belarus): #,##0% -> -123 456% -> -1234.56
Bulgarian (Bulgaria): #,##0% -> -123 456% -> -1234.56
Catalan (Spain): #,##0% -> -123.456% -> -1234.56
Czech (Czech Republic): #,##0% -> -123.456% -> -1234.56
Danish (Denmark): #,##0% -> -123.456% -> -1234.56
German (Germany): #,##0% -> -123.456% -> -1234.56
German (Germany,Euro): #,##0% -> -123.456% -> -1234.56
German (Austria): #,##0% -> -123.456% -> -1234.56
German (Austria,Euro): #,##0% -> -123.456% -> -1234.56
German (Switzerland): #,##0% -> -123'456% -> -1234.56
German (Luxembourg): #,##0% -> -123.456% -> -1234.56
German (Luxembourg,Euro): #,##0% -> -123.456% -> -1234.56
Greek (Greece): #,##0% -> -123.456% -> -1234.56
English (United States): #,##0% -> -123,456% -> -1234.56
English (Australia): #,##0% -> -123,456% -> -1234.56
English (Canada): #,##0% -> -123,456% -> -1234.56
English (United Kingdom): #,##0% -> -123,456% -> -1234.56
English (Ireland): #,##0% -> -123,456% -> -1234.56
...
Japanese (Japan): #,##0.### -> -1,234.56 -> -1234.56
Korean (South Korea): #,##0.### -> -1,234.56 -> -1234.56
Lithuanian (Lithuania): #,##0.## -> -1.234,56 -> -1234.56
Latvian (Lettish) (Latvia): #,##0.### -> -1 234,56 -> -1234.56
Macedonian (Macedonia): #,##0.###;(#,##0.###) -> (1.234,56) -> -1234.56
Dutch (Netherlands): #,##0.### -> -1.234,56 -> -1234.56
Dutch (Netherlands,Euro): #,##0.### -> -1.234,56 -> -1234.56
Dutch (Belgium): #,##0.### -> -1.234,56 -> -1234.56
Dutch (Belgium,Euro): #,##0.### -> -1.234,56 -> -1234.56
Norwegian (Norway,B): #,##0.### -> -1 234,56 -> -1234.56
Norwegian (Norway,NY): #,##0.### -> -1 234,56 -> -1234.56
Polish (Poland): #,##0.### -> -1 234,56 -> -1234.56
Portuguese (Portugal): #,##0.### -> -1.234,56 -> -1234.56
Portuguese (Portugal,Euro): #,##0.### -> -1.234,56 -> -1234.56
Portuguese (Brazil): #,##0.### -> -1.234,56 -> -1234.56
Romanian (Romania): #,##0.### -> -1.234,56 -> -1234.56
Russian (Russian Federation): #,##0.### -> -1 234,56 -> -1234.56
Serbo-Croatian (Yugoslavia): #,##0.### -> -1.234,56 -> -1234.56
Slovak (Slovakia): #,##0.### -> -1 234,56 -> -1234.56
Slovenian (Slovenia): #,##0.### -> -1.234,56 -> -1234.56
Albanian (Albania): #,##0.### -> -1.234,56 -> -1234.56
Serbian (Yugoslavia): #,##0.### -> -1 234,56 -> -1234.56
Swedish (Sweden): #,##0.### -> -1 234,56 -> -1234.56
Thai (Thailand): #,##0.### -> -1,234.56 -> -1234.56
Turkish (Turkey): #,##0.### -> -1.234,56 -> -1234.56
Ukrainian (Ukraine): #,##0.### -> -1.234,56 -> -1234.56
Chinese (China): #,##0.### -> -1,234.56 -> -1234.56
Chinese (Taiwan): #,##0.### -> -1,234.56 -> -1234.56
 
The page was last updated February 19, 2008