Locale-Sensitive JavaScript Constructor
new Intl.NumberFormat();
new Intl.NumberFormat(locales);
new Intl.NumberFormat(locales, options);
Internationalization (I18n) Method Overview
The Intl.NumberFormat constructor returns an object that can then be used to format a number based on locale. If no parameters are passed to the
constructor, then the default locale is used. Fallback is support with the locales parameters, which may be an array of locales.
options can be used to indicate displaying the currency symbol, setting the number of significant digits and other display options.
For example:
var num = 123.567;
var result = new Intl.NumberFormat("de-DE", {maximumFractionDigits: 2}).format(num);
Method returns:
result: 123,57 (German uses comma for decimal separator)
Click here (MDN) for additional details.
I18n Issues
Whether or not calling Intl.NumberFormat is an i18n issue is dependent on how it is being used in the application.
Two possible issues are:
- The formatted number needs to be locale-independent; i.e always the same, regardless the locale.
- A version of the constructor is being called that does not include the locales parameter and so numbers may not be formatted based on the
application's locale.
Suggested Replacement
Don't use the Intl.NumberFormat object to format numbers that should be locale-independent. Instead, call the
Microsoft .NET JavaScript Extension format method or toString
to format numbers independent of locale.
Otherwise, be sure that the locale is set properly and then pass it into the constructor.
Globalyzer will detect this method and report it as an i18n issue. If you have determined that the call is being handled correctly, you can
use Globalyzer's Ignore Comment
functionality to ensure that it isn't picked up in a subsequent scan.
Locale-Sensitive JavaScript Methods
|