Internationalization and localization tools

Locale-Sensitive Java Method


public DecimalFormat()

Internationalization (I18n) Method Overview

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123). All of these can be localized.

To obtain a DecimalFormat for a specific locale, including the default locale, call a NumberFormat factory method such as getInstance(). In general, do not call the DecimalFormat constructors directly.

I18n Issues

This zero-argument constructor produces an instance of DecimalFormat specific to the default locale. There is no locale-sensitive constructor and it is recommended that instead of calling any DecimalFormat constructor that you instead call a NumberFormat factory method, to which you can pass a runtime locale object, allowing you to create an instance of DecimalFormat that is independent of the machine locale.

Suggested Replacement

public static NumberFormat getInstance(
   Locale inLocale)

Instead of:

DecimalFormat format =
  new DecimalFormat();


//Retrieve the runtime user's locale
Locale loc = getUserLocale();

//Now call the NumberFormat factory method
//and pass the locale object
NumberFormat f = NumberFormat.getInstance(loc);

//If it is in fact an instance of DecimalFormat,
//cast it as such and use it as needed
if (f instanceof DecimalFormat) {
    ((DecimalFormat) f).

Please see Number Formatting for more information.

Locale-Sensitive Java Methods


Lingoport internationalization and localization services and software