Locale Specific DecimalFormat

By default, DecimalFormat object is using the JVM’s locale. We can change the default locale while creating the DecimalFormat object using NumberFormat class. In the example below, we’ll use same pattern for two different locale and you can spot the difference in the output.

Example

Open Compiler

importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="###.##";double number =123.45;Locale enlocale  =newLocale("en","US");Locale dalocale  =newLocale("da","DK");DecimalFormat decimalFormat =(DecimalFormat)NumberFormat.getNumberInstance(enlocale);
      decimalFormat.applyPattern(pattern);System.out.println(decimalFormat.format(number));   

      decimalFormat =(DecimalFormat)NumberFormat.getNumberInstance(dalocale);
      decimalFormat.applyPattern(pattern);System.out.println(decimalFormat.format(number));}}

Output

It will print the following result.

123.45
123,45

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *