Groovy 26 – How to get Currency Symbols
To return the currency symbols for any Currency, we can create a global function that will take the currency code as a parameter.
For example, the function will take the code USD and return $.
Create a Global Function with the following parameters:
- Function Name: getCurrencySymbol
- Returns: String
- Parameters:
- Name: CurrencyCode
- Type: String
def map = [:]; for (Locale locale : Locale.getAvailableLocales()) { try { Currency currency = Currency.getInstance(locale); map.put(currency, locale); } catch (Exception e) { // no need to handle exception } } Currency currency = Currency.getInstance(CurrencyCode); String country = map.get(currency); String country1 = country.substring(country.lastIndexOf("_") + 1); Locale locale = new Locale("EN", country1); Currency currency1 = Currency.getInstance(locale); String symbol = currency1.getSymbol(map.get(currency)); return symbol;
This function can be used in code by calling it as below:
adf.util.getCurrencySymbol(java.lang.String)