Below program prints the Currency symbol given the ISO 4217 currency code.
import java.util.*; public class Currency{ public static void main(String args[]) { Currency myInstance = Currency.getInstance(args[0]); System.out.println(myInstance.getSymbol()); } }
Problem: Works fine when the string USD is input. For other inputs like EUR just return the Currency Code.
Sample input , ouput from the program:
input: java Currency USD output: $ input: java Currency EUR output: EUR -> I expect the symbol of Euro here
The first two letters of the ISO 4217 three-letter code are the same as the code for the country name, and, where possible, the third letter corresponds to the first letter of the currency name. For example: The US dollar is represented as USD – the US coming from the ISO 3166 country code and the D for dollar.
Representing money: use BigDecimal , int , or long ( BigDecimal is the recommended default) the int and long forms represent pennies (or the equivalent, of course) BigDecimal is a little more inconvenient to use, but has built-in rounding modes.
getInstance() : java. util. Currency. getInstance() method creates currency instance for Currency code.
The following table provides a list of valid 4217 letter codes.
Currency.getSymbol()
returns the currency symbol relative to the default locale.
Gets the symbol of this currency for the default locale. For example, for the US Dollar, the symbol is
"$"
if the default locale is the US, while for other locales it may be"US$"
. If no symbol can be determined, the ISO 4217 currency code is returned.
Use Currency.getSymbol(Locale locale)
if you want the symbol for a different locale.
System.out.println( Currency.getInstance("USD").getSymbol(Locale.US) ); // prints $ System.out.println( Currency.getInstance("USD").getSymbol(Locale.FRANCE) ); // prints USD System.out.println( Currency.getInstance("EUR").getSymbol(Locale.US) ); // prints EUR System.out.println( Currency.getInstance("EUR").getSymbol(Locale.FRANCE) ); // prints €
(see also on ideone.com).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With