Node 8.0 New characters and adding space after character for Number.prototype.toLocaleString ()
I just upgraded to Node v8.0 and noticed that Number.prototype.toLocaleString () with parameters style: 'currency'
as well as currency
set to any currency behaves differently than any other environment. In Node v7.2.1 as well as Chrome v58.0 my output will look like $5.00
, but in Node 8 it shows with a different currency symbol and extra space as US$ 5.00
. Is this just an update? Where can I find the docs for this? Has Node failed to update toLocaleString
and should be treated as a feature that changes depending on the environment?
const value = 5
value.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
Node 8.0
'US$ 5.00'
Node <8.0 + Chrome
'$5.00'
source to share
Should it be viewed as a function that changes depending on the environment?
Yes, definitely. From ECMA-402 ยง13.2.1 and ยง11.3.4 :
"Calculations are based on string values โโand locations in numeric strings, which are implementation dependent and effective locale [...]"
although he notes that
"It is recommended that implementations use the locale data provided by the Common Locale Data repository (available at http://cldr.unicode.org/ ).
source to share
The problem is related to "icu". The version of Node you are using does not include "full-icu" by default. I found two approaches to solving the problem. The first involves giving Node the complete icu at runtime. The second involves compiling Node from scratch, including full-icu.
Approach to work
Say you have Node 8.11.1
. Install the npm packagefull-icu
globally.
npm install -g full-icu
Read the command line messages to find out where your file was installed full-icu
. You will need it when you specify the -icu-data-dir option when invoking the command node
. For example:
node --icu-data-dir=/some/where/.nvm/versions/node/v8.11.1/lib/node_modules/full-icu Your-App.js
Replace with Your-App.js
your program. You can also install the package full-icu
locally and use it ./nodule_modules/full-icu
for icu-data-dir
.
Link: https://nodejs.org/dist/latest-v8.x/docs/api/intl.html#intl_providing_icu_data_at_runtime
Compilation approach
This approach involves compiling Node from scratch by specifying an include full-icu
in the assembly option . I suggest using NVM :
nvm use 6
nvm uninstall 8
nvm install -s v8 --with-intl=full-icu --download=all
The above will temporarily switch your Node version ahead of time so you can uninstall your Node 8 version before installing the new v8. Don't miss this step! It can take a while to build Node.
(If you don't have version 6 above, install it with nvm install 6
or use another version other than 8)
source to share