Java I18N – NumberFormat Class


Java Internationalization – 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.

import java.text.NumberFormat;
import java.util.Locale;

public class I18NTester {
   public static void main(String[] args) {
      Locale enLocale = new Locale("en", "US");  
      Locale daLocale = new Locale("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

Advertisements

”;

Leave a Reply

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