Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Currency symbol based on ISO 4217 currency cod

Tags:

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 
like image 624
Eternal Learner Avatar asked Oct 22 '10 05:10

Eternal Learner


People also ask

How do I use ISO currency code?

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.

How do you represent currency in Java?

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.

How do you create a currency object in Java?

getInstance() : java. util. Currency. getInstance() method creates currency instance for Currency code.

How many ISO currency codes are there?

The following table provides a list of valid 4217 letter codes.


1 Answers

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

like image 159
polygenelubricants Avatar answered Sep 27 '22 21:09

polygenelubricants