views:

1068

answers:

4

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"?

+1  A: 

It looks like Java 7 will allow you to override currencies in a properties file.

Mr. Shiny and New
But that properties file only contains ISO 4217 related information. And ISO 4217 does not contain currency symbols.
A: 

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

System.out.println(curr.getSymbol(trLocale).substring(1));
Doomspork
+2  A: 

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
Being able to use the old currency code is a bit of good fortune I suppose. Nice hack!
extraneon
A: 

Resurrection for further reference:

You can use DecimalFormatSymbols to change the Currency for Formatting purposes

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

Or you can appeal to SPI and plug-in your currency symbol (maybe by creating your own country variant like new Locale("tr","TR","try")). In order to do that you should provide a implementation to java.util.spi.CurrencyNameProvider and register it with Java extension mechanism. Check out http://java.sun.com/javase/6/docs/api/java/util/spi/LocaleServiceProvider.html

Anthony Accioly