4

I'm trying to format a price using Intl.NumberFormat(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat) in Norwegian Krone.

The instruction I'm using is:

console.log(new Intl.NumberFormat('no-NO', { style: 'currency', currency: 'NOK' }).format(123));

But as you can see, the output is "123,00 NOK" while I was expecting "123,00 kr". Have anyone encountered the same problem ?

Thanks for your help

Hyukchan Kwon
  • 382
  • 1
  • 5
  • 20
  • 1
    Look at the `currencyDisplay` option – Heretic Monkey Feb 03 '20 at 16:08
  • Yep I looked at the option but the default value is "symbol" anyway. I shouldn't have the code displayed. – Hyukchan Kwon Feb 03 '20 at 16:09
  • 2
    It will show the ISO currency code when the symbol is not available. So it seems that you test it on a browser where the "kr" symbol is not available. It shows "NOK" if I test it in Chrome, but it shows "kr" to me when using Firefox. – Ivar Feb 03 '20 at 16:40
  • @Ivar I just tried in firefox. Still getting NOK . – Menelaos Feb 03 '20 at 16:45
  • 1
    @MenelaosBakopoulos Odd. [It shows kr for me](https://i.stack.imgur.com/LHePs.png) on FF 72.0.2 (64-bit) (Windows 10). – Ivar Feb 03 '20 at 16:48
  • @Ivar yeah I've been seeing that there can be a difference from one javascript runtime environment to another : https://stackoverflow.com/questions/55183776/different-behaviour-of-intl-numberformat-in-node-and-browser/55183777#55183777 Though, I hoped nowadays browsers would all include every "ICU data"... And even if I test in a node environment including all ICU data, it doesn't work for NOK... – Hyukchan Kwon Feb 03 '20 at 16:54
  • @Ivar, Same output for me in Firefox (64-bit) Ubuntu – Mohammad Faisal Feb 07 '20 at 12:06
  • @Ivar that is not case. The reason why the `kr` is not displayed by the browser is not because the browser does not support `kr`. Those are just ASCI letters `0x6B` and `0x72`. @HyukchanKwon you can use `Norwegian bokmål` with locale `nb-NO` to have `kr` in all browsers, but you'll have the currency symbol at the front and a space for thousand separator like `kr 123 456 789,00`. Also for some browsers like Firefox, `Norwegian nynorsk` `nn-NO` locale will have `kr` at the end but again you'll have space for thousand separators. – Christos Lytras Feb 13 '20 at 00:19
  • If we look at [`icu4c/source/data/locales/no_NO.txt`](https://github.com/unicode-org/icu/blob/master/icu4c/source/data/locales/no_NO.txt) there is alias to `nb_NO` and inside [`icu4c/source/data/locales/nb_NO.txt`](https://github.com/unicode-org/icu/blob/master/icu4c/source/data/locales/nb_NO.txt) there is `Version{"36.1"}` and inside [`icu4c/source/data/locales/nb.txt`](https://github.com/unicode-org/icu/blob/master/icu4c/source/data/locales/nb.txt) there is `currencyFormat{"¤ #,##0.00"}` with `symbols` for `decimal{","}` and `group{" "}`. To me it seems there are no data for `no-NO` locale. – Christos Lytras Feb 13 '20 at 00:40
  • @ChristosLytras I'm not completely following you. I was referencing [the specification](https://tc39.es/ecma402/#sec-currency-codes) which states that "_Where a localized currency symbol is not available, the ISO 4217 currency code is used for formatting._". In the case of OP it shows the ISO 4217 currency code where other locales show the actual symbol, which is an indication to me that the symbol is not available. Am I missing something? – Ivar Feb 13 '20 at 08:43
  • If you only want to get "kr", this can be the workaround: `console.log(new Intl.NumberFormat('sv', { style: 'currency', currency: 'SEK'}).format(number));` – Zain Ul Abideen Feb 14 '20 at 07:59

5 Answers5

4

Use currencyDisplay: 'narrowSymbol' This will prompt numberFormatter to show the short form for NOK.

Also, the correct locale for norwegian is nb-NB or nb-NY, this way you get space separator for groups and comma for decimals.

console.log(new Intl.NumberFormat('nb-NB', { style: 'currency', currency: 'NOK' , currencyDisplay: 'narrowSymbol' }).format(123));
Sølve T.
  • 4,159
  • 1
  • 20
  • 31
3

This is a chromium family mistake.
There is nothing to do except posting a ticket to developers reports, if nobody already done that

The only possible thing to do is to use a patch, which you can change when the chrome teams have fixed this problem.

It should works everywhere without negative impact, even if it remains after the correction of chrome team.

What I suggest:

const IntlNfNO = new Intl.NumberFormat('no-NO', { style: 'currency', currency: 'NOK'  })
  ,   Intl_NOK = v => IntlNfNO.format( v ).replace('NOK', 'kr') 
  ;


console.log( Intl_NOK(123) );
console.log( Intl_NOK(456) );
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
2

You could use property currencyDisplay: "name". This not shows in my version long name, still I guess it is closer to expected solution.

console.log(new Intl.NumberFormat('no-NO', { style: 'currency', currency: 'NOK' , currencyDisplay: "name" }).format(123));
SkorpEN
  • 2,491
  • 1
  • 22
  • 28
  • Sorry, this isn't what I want as a result. I really want to know why currency code 'NOK' isn't correctly handled with the symbol... – Hyukchan Kwon Feb 07 '20 at 14:23
0

As per the spec, Intl doesn't support shortName for Norwegian Currency. In-order to format the currency value, you should use formatToParts to format the currency values as per our need.

function getNorwegianCurrencyFormat(value) {
  const norwegianCurrencyShortForm = "Kr"
  const norwegianCurrencyFormat =  new Intl.NumberFormat('no-NO', { style:     'currency', currency: 'NOK' });
  return norwegianCurrencyFormat.formatToParts(123).map(({type, value}) => type === "currency" ? norwegianCurrencyShortForm : value).join("");  
}

console.log(getNorwegianCurrencyFormat(123));

I hope this helps you.

prasana kannan
  • 646
  • 6
  • 12
0

One workaround could be if you only wanna get kr:

console.log(new Intl.NumberFormat('sv', { style: 'currency', currency: 'SEK'}).format(number));

Zain Ul Abideen
  • 1,617
  • 1
  • 11
  • 25