-1

I have a table of 3-alpha currency codes for every available currency, e.g: USD, JPY, EUR, etc.

But it's a bit long-winded to display the 3-alpha codes on a web page when most people simply display a currency symbol ($, ¥, , etc).

I haven't been able to find a free Java library to convert the 3-alpha currency codes to their corresponding currency symbols? The Java Locale feature is limited and cumbersome.

So I've decided to manually add a new column to my existing table and add the appropriate currency symbols myself.

However, I am not sure in what format to represent them in the table so that they can be easily outputted on a JSP page? Should I copy-paste the symbols from Wikipedia? Or, if I get Unicode values for the symbols somewhere, is it easy to have a JSP convert the Unicode values to the symbols? I haven't worked with Unicode before.

Nagano
  • 1
  • 2
  • You can use [icu4j](http://icu-project.org/apiref/icu4j/com/ibm/icu/util/Currency.html#Currency%28java.lang.String%29). [ICU](http://site.icu-project.org/) is a mature, widely used set of C/C++ and Java libraries providing Unicode and Globalization support for software applications. – Srikanth Venkatesh Aug 31 '11 at 14:18

4 Answers4

2

Did you try getSymbol on Currency as per javadocs?

Why is it cumbersome?

Ula Krukar
  • 12,549
  • 20
  • 51
  • 65
Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
  • Maybe not so cumbersome but certainly not as straight forward as you make it feel. He must know the ISO 4217 code of the currency, also the method don't say anything about only returning the desired symbol in 1char length but can be return as 3chars which he wanted to avoid if presented in a special locale. –  Aug 31 '11 at 14:18
  • 1
    I think this is a good suggestion. It's true that the result of `getSymbol()` is not always a single character, but the fact is that currency symbols are not always single characters. That's life. – Tom Anderson Aug 31 '11 at 15:08
  • @Tom Anderson exactly! I would love to see single character symbols for AFN (Afghan Afghani) or AOA (Angolan Kwanza), to name a few :-) – Ashkan Aryan Aug 31 '11 at 21:59
0

Maybe you can have the currency symbols in your language file.

Updated: Removed the text part with the db

  • Why is storing currency symbols in the database considered bad? Who considers it bad? I consider it good. – Tom Anderson Aug 31 '11 at 15:06
  • @Tom Anderson To be honest I haven't given it much thought maybe it isn't considered bad. It was just something I picked up from this thread, http://stackoverflow.com/questions/783971/storing-currency-symbols-in-a-database-table, there is an undisputed comment stating "Currency symbols do not belong into the database, at all." if you know otherwise it may be good to add a comment there to so more users don't get fooled. –  Aug 31 '11 at 15:11
0

If you choose to create a lookup table from a currency abbreviation to a symbol, the best choice is to use an HTML entity. There are two variants, and you'll probably want to use both.

First, for common currencies, there are explicit entities, such as €. You'll find a table of them here (some symbols will be in the "Latin 1" page, others will be in the "Other Special Characters" page).

Second, for non-common currencies, you should use a Unicode escape. The general form is &#xnumber;, where number is the hexadecimal Unicode codepoint value. There is a block of currency symbols, although it's possible that the symbols you want are included in another code block. The starting point for finding a code block is here.

Do not simply paste symbols from Wikipedia, unless you understand how encodings work, along with (1) what encoding your files actually use, and (2) what encoding your web-server claims they use.

parsifal
  • 1,507
  • 8
  • 7
-1

The NumberFormat class is where you will want to look. This will be locale specific and be able to provide you with the proper numbering. Here is an example.

NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
String usFormat = us.format(56.45);
System.out.println(usFormat);

NumberFormat uk = NumberFormat.getCurrencyInstance(Locale.UK);
String ukFormat = uk.format(56.45);
System.out.println(ukFormat);

your output will look like the following

$56.45
£56.45
Sean
  • 7,597
  • 1
  • 24
  • 26
  • part of the question was the mapping between the codes in the database and the symbol. Also are your code guaranteed to output all symbols as one characters for all locales and not for example AUS$ –  Aug 31 '11 at 14:25