How to determine where to place the currency symbol when formatting a number?
Asked Answered
B

4

7

I am not sure if the same question is asked by any other. I have not seen this question in stackoverflow so far, hence posting the same.

I have a requirement where I need to format the numbers based on the currency.

enter image description here

Let me explain the above table to give you more context of my question. For UnitedStates, numbers are formatted as USD976,543.21, here the currency symbol is USD which is placed in the front before numbers. This is little different for fr-FR locale, here symbol is placed after the digits.

All my formatting is happening in the front end and not the backend. I am giving a format in the form of # to the FE, then our home grown code formats the digits in that format. Example, for United states, I am supplying USD###,###,###.####. Here, I need to let the front end know the below information so that front end formats the same in a prompt manner. My question is: Is there any way to get the below information in the backend:

  1. Where to place the currency, start of the number or towards ending?
  2. Is there any space between number and symbol? Look at USD976,543.21 and 976 543,21 € . There is space between currency symbol and number for EURO but not for USD.

I am using java.util.Currency.

Beleaguer answered 3/5, 2017 at 6:6 Comment(0)
C
10

I found this question while searching for a way to determine whether the currency symbol should be placed at the start or end of a currency for a specific locale. For others who might be looking for something similar, I ended up creating the following class to test this:

public class Test {   

    private static Locale[] locales = Locale.getAvailableLocales();
    static double decimal = 789456123.098;

    public static void main(String[] args) {

        for(Locale locale : locales){
            DecimalFormat decimalFormatter = (DecimalFormat)NumberFormat.getInstance(locale);
            NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
            DecimalFormatSymbols symbols = decimalFormatter.getDecimalFormatSymbols();
            String currencySymbol = symbols.getCurrencySymbol();
            String formattedCurrency = currencyFormatter.format(decimal);
            System.out.print(formattedCurrency + ";" + locale.getDisplayCountry() + ";" + locale.getDisplayLanguage() );
            System.out.println(";" + currencySymbol + ";" + currencySymbolAtStart(currencySymbol, formattedCurrency, true));
        }

    }

    private static boolean currencySymbolAtStart(String currencySymbol, String formattedCurrency, boolean defaultAtStart){

        if(formattedCurrency.startsWith(currencySymbol)){
            return true;
        }else if(formattedCurrency.endsWith(currencySymbol)){
            return false;
        }else{
            return defaultAtStart;
        }
    }

}

Pasting the results into a spreadsheet looks something like this:

Java currency symbol start position

There is one caveat here to be aware of. For languages that are read right to left, the currency symbol might appear on the right in the formatted text, yet technically, since you are reading from right to left, the currency still "starts with" the currency symbol, hence the result will return "true". See Egypt-Arabic in the screen snip above...

Curcuma answered 16/3, 2019 at 18:32 Comment(0)
H
1

Great explanation and code is here: https://drivy.engineering/multi-currency-java/

I would say use the standard, instead of pleasing your designers.

Handsome answered 26/7, 2019 at 15:12 Comment(0)
C
0

You can use Globalizejs for your requirement

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data

It's pretty easy to start with and you need not worry about placing the currency code in front or at the end, The library will take care according to locale and currencyCode.

Example:

var formatter;

Globalize.locale( "en" );
formatter = Globalize.currencyFormatter( "USD" );

formatter( 9.99 );
// > "$9.99"

in different locale:

var deFormatter = Globalize( "de" ).currencyFormatter( "EUR" );
deFormatter( 9.99 );
// > "9,99 €"

enter image description here

Concoct answered 3/5, 2017 at 6:15 Comment(2)
Yes, I read that but this line All my formatting is happening in the front end and not the backend. caught my eye. hence gave him an easy well-evolved solution. :)Concoct
Thank you @NeerajJain for your comments. I have a home grown code to do the number formatting. What I am missing is, the two questions that I asked in the question. How can determine the placement of currency symbol, is it first or last? and Is there any space between currency code and symbol?Beleaguer
G
0

The NumberFormat.getCurrencyInstance() can be used to format a number according to the currency of a particular locale in the backend. And the formatted currency can be passed to FE for rendering. For example:

    double num = 976_543.21;

    NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(new Locale("fr","FR"));
    System.out.println(defaultFormat.format(num)); //output 976 543,21 €

    Locale us = new Locale("en", "US");
    NumberFormat usFormat = NumberFormat.getCurrencyInstance(us);
    System.out.println(usFormat.format(num)); //output $976,543.21
Gann answered 3/5, 2017 at 7:18 Comment(1)
I am not doing number formatting in the BE where as I need to do the formatting in the front end. Inorder to do that I need couple of things more, 1. Where to place the currency, start of the number or toward ending, how can i determine this. 2. Is there any space between number and symbol.Beleaguer

© 2022 - 2024 — McMap. All rights reserved.