Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intl.NumberFormat with 'en-CA' and 'CAD' doesn't behave like other implementations #15265

Closed
epayet opened this issue Sep 8, 2017 · 8 comments
Labels
i18n-api Issues and PRs related to the i18n implementation.

Comments

@epayet
Copy link

epayet commented Sep 8, 2017

  • Version:
    8.4.0
  • Platform:
    Linux 6b594c82153b 4.9.41-moby deps: update openssl to 1.0.1j #1 SMP Wed Sep 6 00:05:16 UTC 2017 x86_64 GNU/Linux
  • Subsystem:
    Ubuntu (From Docker image node:8.4.0)

Node behaves differently wrongly when formatting the 'CAD' currency using the 'en-CA' locale.
Node returns CA$1.00 whereas other implementations return $1.00.

root@5d8e11364deb:/# node --version
v8.4.0
root@5d8e11364deb:/# node
> new Intl.NumberFormat('en-CA', {style: 'currency', currency: 'CAD'}).format(1.00);
'CA$1.00'

From Chrome:

screen shot 2017-09-08 at 12 22 54

From Firefox:

screen shot 2017-09-08 at 12 24 24

The polyfill behaves correctly:

> var IntlPolyfill = require('intl')
> new IntlPolyfill.NumberFormat('en-CA', {style: 'currency', currency: 'CAD'}).format(1.00);
'$1.00'

I'm using the small-icu, so I know that only basic english is supported, so this is probably why en-US and en-CA behave the same way:

> new Intl.NumberFormat('en-CA', {style: 'currency', currency: 'CAD'}).format(1.00);
'CA$1.00'
> new Intl.NumberFormat('en-US', {style: 'currency', currency: 'CAD'}).format(1.00);
'CA$1.00'

The polyfill page suggests to use the polyfill only if the locale is not supported, which makes sense, but according to Node, en-CA is supported:

> Intl.NumberFormat.supportedLocalesOf(['en-CA']) // `en-CA` is supported
[ 'en-CA' ]
> Intl.NumberFormat.supportedLocalesOf(['it']) // `it` is not supported
[]

Since it's marked as supported, I'm using the default Intl, and then have a bug when displaying numbers, using en-CA. I am then forced to use the polyfill by default.

Also, I first thought this was related to this issue (#15223), but I tried on an older version of node, and it does the same thing:

root@ac5d58c9b566:/# node --version
v7.9.0
root@ac5d58c9b566:/# node
> new Intl.NumberFormat('en-CA', {style: 'currency', currency: 'CAD'}).format(1.00); // wrongly formatted
'CA$1.00'
> Intl.NumberFormat.supportedLocalesOf(['en-CA']) // `en-CA` is supported
[ 'en-CA' ]

How do think it should behave here? Should en-CA be marked as not supported if using the small ICU? Or is that just a bug?

@epayet epayet changed the title Intl.NumberFormat with 'en-CA' and 'CAD' doesn't behave like browser implementations Intl.NumberFormat with 'en-CA' and 'CAD' doesn't behave like other implementations Sep 8, 2017
@TimothyGu
Copy link
Member

The way the ECMA-402 spec defines supportedLocalesOf is through the LookupSupportedLocales abstract operation, which allows requested locales to be added to the resulting array as long as the most general language tag (e.g. en) is supported. small-icu does support en. So, this isn't a bug, just a side effect of small ICU.

@Fishrock123 Fishrock123 added the i18n-api Issues and PRs related to the i18n implementation. label Sep 8, 2017
@bnoordhuis
Copy link
Member

Looks like this has been answered. Closing, reopen if that is a mistake.

@epayet
Copy link
Author

epayet commented Sep 11, 2017

Ok sure, I'll take that as a side effect then.
Thanks for the answer.

@jnrepo
Copy link

jnrepo commented Jul 17, 2019

Hi @TimothyGu , so my friend and I are on two different machines. Both of us are running node 10.16.0; however, when we run new Intl.NumberFormat('en-CA', { style: 'currency', currency: 'CAD' }).format(1.00) we seem to get two different results. On theirs the result is $1.00, but on mine it is always CA$1.00. Why is this not consistent?

@bnoordhuis
Copy link
Member

One of you probably has the full-icu package installed, the other hasn't.

@telmaantunes
Copy link

I seem to have the same issue as @jnrepo but with en-AU

@JayMehta97
Copy link

So what is the solution to this?

@kevindantas
Copy link

@jnrepo the probable reason why you don't have consistency on different machines is the locale of each machine. If one machine is using en-US, the formatter will add the CA$ to be explicit on which currency you're using, but if the machine is using en-CA it won't add anything as the machine is likely to be in Canada.

const formatterCA = new Intl.NumberFormat('en-CA', {
    style: 'currency',
    currency: 'CAD',
  });

const formatterUS = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'CAD',
  });

formatterCA.format(399) // $399.00
formatterUS.format(399) // CA$399.00

The same goes for the other way around, if you're using en-CA and try to display US dollars it will add US to be more explicit

const formatter = new Intl.NumberFormat('en-CA', {
    style: 'currency',
    currency: 'USD',
  });

formatter.format(100); // US$100.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i18n-api Issues and PRs related to the i18n implementation.
Projects
None yet
Development

No branches or pull requests

8 participants