Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

currency formatter

On my localhost I am using this function to give me a value in currency format
  const currencyFormatter = new Intl.NumberFormat("en-ZA", {
    style: "currency",
    currency: "ZAR",
    minimumFractionDigits: 2,
  });

Open in new window

This works as expected. However, after deploying to Heroku instead of the output being:
R 6,500.00

Open in new window

it is
 ZAR 6,500.00

Open in new window

is this a heroku thing or issue with the code?
Avatar of ste5an
ste5an
Flag of Germany image

Well, without knowing your systems in detail. it means that the localization part for South Africa on your Heroku system is not correctly configured or triggered. Actually it cannot resolve the currency symbol.

So you should look into the localization handling of Heroku and how it interacts with the front-ends.
whats is the code that prints/uses this format?
I am getting

  const number = 123456.789;
  const currencyFormatter = new Intl.NumberFormat("en-ZA", {
    style: "currency",
    currency: "ZAR",
    minimumFractionDigits: 2,
  });
  
  console.log(currencyFormatter.format(number));

Open in new window

R 123 456,79

on my Win 10 Ent - Chrome / 64 Bit

https://jsfiddle.net/HainKurt/m73v5qyt/
@HainKurt: Heroku is a multi-language/system. Especially supporting node.js. Thus my guess for the Heroku installation.

ICU Locale “English (South Africa)” (en_ZA)

https://www.localeplanet.com/icu/en-ZA/index.html

looks like, these are using some local files (ICU Fıles) for all those formatting...

if you have wrong info, you need to update whatever you use...
browser/node.js/app/library...
Avatar of Crazy Horse

ASKER

@HainKurt, it works fine on my local Node.js installation, it is just on the Heroku server that it is doing this but I don't know how to resolve the issue. I am on the Heroku free tier and I don't see anywhere or know how to fix it so I was wondering if there is a workaround with the javascript code I posted or advice on how to sort it out on Heroku.
they need to update some libraries I guess...

https://www.localeplanet.com/java/en-ZA/index.html

anyways, you can replace "ZAR" with "R" for temp fix...

currencyFormatter.format(number).replace("ZAR", "R")

Open in new window

replace :
currency: "ZAR",
by :
currency: "R",

or anything else, for example :
currency: "AIR",

to confirm it use ZAR as it doesn't find the currency until you install it
replace :
currency: "ZAR",
by :
currency: "R", 

  const number = 123456.789;
  const currencyFormatter = new Intl.NumberFormat("en-ZA", {
    style: "currency",
    currency: "R",
    minimumFractionDigits: 2,
  });
  
  console.log(currencyFormatter.format(number));

Open in new window

User generated image
@HainKurt, you still want to run this on your computer in a browser instead on Heroku with NodeJS
@leakim971

I thought this is js and will run on browser/clients...
even if it runs on that server only, changing currency probably will not work,
they all run the same specs...

thats why, I suggested, get whatever it returns, and do a string replace to fix it...
if updating libraries is not an option...
I did some test on my pc

and currency format accepted is 3 char

full list is here

https://www.currency-iso.org/en/home/tables/table-a1.html

but you can use any other 3 letter code
if it is less than 3, or any non alpha char, it is not accepted...
this is standard, no matter where it is run...

so, currency="R" will not work
but using any 3 letter code,
like currency="ANY" and then replace "ANY" with "R" will work...


Guys, your mixing the currency code with the currency symbol.

When you see the currency code instead of the symbol in the front-end, then this normally means, that the system has no lookup for the symbol and is using the code. Or the the localization table is not complete and it contains as symbol the code.

Doing a replace on the currency code is plastic surgery at the wrong point.

You need to look into the localization system. That's work, but you need to do it, to fully understand it.
@leakim971, I did see that as well. I am already using en-ZA. Like I said, it works as expected on my local machine but not on Heroku, so something could be wrong there but I have no idea how to fix it on Heroku. 
As I already wrote, the fact that it works on your machine means in this case that the code is fine, It is a setup or configuration issue of Heroku. The bug as indicated by leakim971 in metabase confirms this in my opinion.

Thus: I would talk the providers technical team,
here the file : https://analytics.utah.gov/bi/lib/ecma402/0.2.3-dev/cldr/en-ZA/currencies.json
It look fine

It seems by default NodeJS don't come with all the locales
you need to install the full-icu package : https://github.com/unicode-org/full-icu-npm

npm install full-icu

Open in new window




Thanks leakim971, I will give that a try
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.