Locale-Sensitive JavaScript Constructor
Number(value);
Internationalization (I18n) Method Overview
The Number constructor returns a Number object by converting the value parameter to a number. In the case where value is
a Date object, it returns the UTC time value; if value is a floating point string, it assumes the decimal separator is a period, rather than using the
locale's setting.
Click here (w3schools) and
here (MDN) for additional details.
I18n Issues
Whether or not calling Number is an i18n issue is dependent on how it is being used in the application.
Two possible issues are:
- In the case of a Date object being converted to a numeric time value, the UTC time is displayed without using locale or the application's time zone .
- In the case of a string being converted to a numeric value, the floating point string is based on user's locale, which may not use a period as the
decimal separator.
Suggested Replacement
To set the time zone and format dates based on locale, call one of the locale-sensitive date methods:
toLocaleDateString
toLocaleString
toLocaleTimeString
To properly parse numeric strings based on locale, you could call parseLocale if you are using Microsoft's
.NET Framework JavaScript Extensions. Otherwise, you will have to use a 3rd party JavaScript library to parse locale-sensitive numeric strings based on locale. Once you
do have a number, you can format the number based on locale using the
Intl.NumberFormat class, which
has support for locale.
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
|