11

I'm trying to print prices in Turkish Liras (ISO 4217 currency code TRY) with Java.

When I do

Currency curr = Currency.getInstance("TRY");
Locale trLocale = new Locale("tr", "TR");
System.out.println(curr.getSymbol(trLocale));

the output is: "YTL".

However, the currency symbol for Turkish Lira has recently changed from "YTL" to "TL" (as can be seen on the Wikipedia page for Turkish Lira). Formatting with NumberFormat gives a similar result.

I really don't want to write yet another Currency class, especially when Java has one built-in.

Is there a way to override Java's default currency symbol for TRY to "TL"?

user110665
  • 111
  • 1
  • 3
  • 2
    This doesn't immediately help, but file a bug and it'll probably be fixed in the next Java update. – Michael Myers May 21 '09 at 18:00
  • 1
    there is no way other than to create your own wrapper for Currency. Currency is a final class so it cannot even be extended to override getSymbol for your specific case. Either live with YTL and report the bug (and hope its fixed in the next patch) or implement your own solution. – z - May 21 '09 at 18:28

4 Answers4

7

Resurrection for further reference:

You can use DecimalFormatSymbols to change the Currency for formatting purposes:

Locale trLocale = new Locale("tr", "TR");
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(trLocale);
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(trLocale);
dfs.setCurrencySymbol("TL");
decimalFormat.setDecimalFormatSymbols(dfs);

Or you can appeal to SPI and plug-in your currency symbol (maybe by creating your own country variant like new Locale("tr","TR","tl")). In order to do that you should provide a implementation to java.util.spi.CurrencyNameProvider and register it with Java extension mechanism. Check out LocaleServiceProvider Documentation.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
3

Amusingly, according to Wikipedia, the currency code "TRL" is obsolete (the old code for Turkish lira). I have not upgraded to the latest jdk, (on 1.5.0_10), but

Currency curr = Currency.getInstance("TRL");
Locale trLocale = new Locale("tr", "TR");
System.out.println(curr.getSymbol(trLocale));

Prints:

TL

So perhaps write something of your own to map existing codes to obsolete codes and adjust and test as needed.

benson
  • 203
  • 1
  • 8
  • Being able to use the old currency code is a bit of good fortune I suppose. Nice hack! – extraneon May 22 '09 at 11:43
  • Well they dropped six zero's off the old currency, so the difference is in fact a million to one... be careful what you wish for! – vikingsteve Feb 27 '13 at 07:50
2

In Java 7 you can override currencies in a properties file. If you are relying on the ISO-4217 symbol, it can be specified/overridden there.

Mr. Shiny and New 安宇
  • 13,822
  • 6
  • 44
  • 64
  • 1
    But that properties file only contains ISO 4217 related information. And ISO 4217 does not contain currency symbols. – user110665 May 21 '09 at 20:35
0

For the mean time while you're waiting on Java:

System.out.println(curr.getSymbol(trLocale).substring(1));
doomspork
  • 2,302
  • 1
  • 17
  • 24