Location of currency symbol for a particular currency in java

被刻印的时光 ゝ 提交于 2020-12-10 08:01:07

问题


I know the way to get the Currency Object and other details for a currency in java using locale and NumberFormat class. But I am not able to find anything in the API to know whether currency symbol is display at start or end

E.g. 10 in US is $10 where $ is in the start of number) 10 in Zloty (polish currency) is 10 z (z to represent zloty symbol though actual symbol is different).

Is there any property in number format or currency class which can help me find whether currency symbol is placed in start or end?


回答1:


I don't seem to have very many Locales loaded... but france uses a trailing symbol, taiwan uses a leading symbol.

public class MyCurrency {
    public static void main(String[] args) {
        System.out.println(format(Locale.FRANCE, 1234.56f));
        System.out.println(format(Locale.TAIWAN, 1234.56f));
    }

    public static String format(Locale locale, Float value) {
        NumberFormat cfLocal = NumberFormat.getCurrencyInstance(locale);
        return cfLocal.format(value);
    }
}

Now if you really want to know if the currency symbol is at the beginning or the end, use the following as a starting point. Note the bPre variable...

public String format(Locale locale, Float value) {

    String sCurSymbol = "";
    boolean bPre = true;
    int ndx = 0;

    NumberFormat cfLocal = NumberFormat.getCurrencyInstance(locale);
    if (cfLocal instanceof DecimalFormat) { // determine if symbol is prefix or suffix
        DecimalFormatSymbols dfs =
                ((DecimalFormat) cfLocal).getDecimalFormatSymbols();
        sCurSymbol = dfs.getCurrencySymbol();
        String sLP = ((DecimalFormat) cfLocal).toLocalizedPattern();


        // here's how we tell where the symbol goes.
        ndx = sLP.indexOf('\u00A4');  // currency sign

        if (ndx > 0) {
            bPre = false;
        } else {
            bPre = true;
        }

        return cfLocal.format(value);

    }
    return "???";
}

Credit - I ripped the code from this page. http://www.jguru.com/faq/view.jsp?EID=137963




回答2:


Here's a Groovy snippet showing where NumberFormat is fetching the Locale data from which decides whether to make the currency symbol a prefix or a suffix.

The "CLDR" naming indicates that the underlying locale data is taken from http://cldr.unicode.org/

// Taken from sun.util.locale.provider.NumberFormatProviderImpl

// Configuration .java files are stored within the JDK's src.zip under directory:
// jdk.localedata/sun/text/resources/cldr/ext
// Look for the "NumberPatterns" key and see the position of \u00a4 (currency symbol)

import sun.util.locale.provider.CalendarDataUtility
import sun.util.locale.provider.LocaleProviderAdapter

locale = Locale.ENGLISH
type = LocaleProviderAdapter.Type.CLDR

Locale override = locale.getUnicodeLocaleType("nu") == null ?
        CalendarDataUtility.findRegionOverride(locale) :
        locale;

LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
String[] numberPatterns = adapter.getLocaleResources(override).getNumberPatterns();

print(numberPatterns[1])


来源:https://stackoverflow.com/questions/3809078/location-of-currency-symbol-for-a-particular-currency-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!