Is there a way to get consistent output when fromatting currency based on locale

Deewendra Shrestha :

We are formatting numeric values based on locale but we are finding out that the output generated are different when we generate it in different programming languages. And I am not sure which output is the right one either.

In java:

//https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html
import java.text.NumberFormat

double money = 987654.32
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("ar", "DZ"))
System.out.println(formatter.format(money))
formatter = NumberFormat.getCurrencyInstance(new Locale("bg", "BG"))
System.out.println(formatter.format(money))

Outputs:

د.ج.‏ 987,654.32
лв.987 654,32

In javascript:

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
var number = 987654.32;
console.log(new Intl.NumberFormat('ar-DZ', { style: 'currency', currency: 'DZD' }).format(number));
console.log(new Intl.NumberFormat('bg-BG', { style: 'currency', currency: 'BGN' }).format(number));

Outputs:

د.ج.‏ 987.654,32
987654,32 лв.

In c#:

var amount = 987654.32M;
var cultureInfo = CultureInfo.GetCultureInfo("ar-DZ");
var formattedAmount = String.Format(cultureInfo, "{0:C}", amount);
Console.WriteLine(formattedAmount);

cultureInfo = CultureInfo.GetCultureInfo("bg-BG");
formattedAmount = String.Format(cultureInfo, "{0:C}", amount);
Console.WriteLine(formattedAmount);

Outputs:

د.ج.‏ 987,654.32
987 654,32 лв.

As you can see the thousands separator is different as well as placement of currency symbol as well. Is there a way we can fix for the discrepancy, or do you know of more reliable library that should probably be trusted more than these solutions?

Basil Bourque :

Localization is a complicated process. There are over two hundred languages with over five hundred regional variants. While this already makes a difficult academic task, add on political issues and governmental dictates. Delineating what is linguistically and culturally appropriate is a difficult task. And even then, they change over time as norms shift and evolve.

Adding to that difficulty is the fact that in computing we have multiple sources from which to get the linguistic and cultural definitions. And each of those sources changes with later versions.

So adding this all together, it is no wonder that you see different localization results across programming languages and deployment platforms.

A common source of localization definitions is from the Standard C Library available in most every OS. The providers are known to be overwhelmed by the complexity and detail of the problem, and do not fare well. You should avoid this source where you can.

On some platforms such as macOS we can find higher level frameworks with better handling of localization information.

The best cross-platform source is the CLDR definition files and the ICU library, both provided by the Unicode Consortium. Later versions of Java now use these by default. Ditto for some database systems such as Postgres and 4D.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154464&siteId=1