Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 498
nimi Avatar asked May 03 '17 06:05

nimi


People also ask

How do you apply the currency Number Format?

Tip: To quickly apply the Currency format, select the cell or range of cells that you want to format, and then press Ctrl+Shift+$. Like the Currency format, the Accounting format is used for monetary values. But, this format aligns the currency symbols and decimal points of numbers in a column.

How can you make Excel automatically apply the currency format to a Number?

Select the cells that you want to format and then, in the Number group on the Home tab, click the down arrow in the Number Format box. Choose either Currency or Accounting.

What is the correct way to write currency?

For US dollars, the symbol '$' is sufficient abbreviation, unless there is a mixture of dollar currencies in the text. For other dollar currencies, '$' should be prefixed with the country abbreviation. For all other currencies, write the figure first followed by the currency name, for example, '100 million yuan'.

Do you put USD before or after the Number?

It is composed of the country code ( US ), followed by the letter “D” for “dollar.” Write the dollar figure first, followed by a non-breaking space and the code: 350 000 USD.


1 Answers

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...

like image 145
smitty1 Avatar answered Nov 14 '22 23:11

smitty1