NumberFormat Class

The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country. For example, In Denmark fractions of a number are separated from the integer part using a comma whereas in England they use a dot as separator.

Example – Format Numbers

In this example, we’re formatting numbers based on US locale and Danish Locale.

Open Compiler

importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getInstance(daLocale);System.out.println(numberFormat.format(100.76));

      numberFormat =NumberFormat.getInstance(enLocale);System.out.println(numberFormat.format(100.76));}}

Output

It will print the following result.

100,76
100.76

Comments

Leave a Reply

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