English 中文(简体)
JAVA I18N - NumberFormat Class
  • 时间:2024-12-22

Java Internapzation - NumberFormat Class


Previous Page Next Page  

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.

IOTester.java

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

pubpc class I18NTester {
   pubpc 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
Print