18

I'm trying to add bitcoin as a currency to display on my site. I've got exchange rates and everything, but I keep getting an IllegalArgumentException whenever I use java.util.Currency.getInstance("BTC"). This makes sense since it's not included in the list of ISO 4217 currency codes, and also not in Java 7. I've seen a couple of options, but nothing that really solves my issue.

  1. According to the Java platform docs, you can override a specific locale's currency by creating a file $JAVA_HOME/lib/currency.properties. This is a problem since bitcoin is not tied to a specific locale, nor should it be used in place of any country's currency.

  2. Another similar situation was presented in this StackOverflow post, where China had a second currency code to be used, so the solution was to build your own currency.data file that added a second currency for the China locale. This is better, but there is still the issue of tying a currency to a locale.

Has anyone run into this problem or found a workaround? I know bitcoin is relatively new, but it'd be cool to be able to display prices in bitcoin format.

Community
  • 1
  • 1
tedski
  • 2,253
  • 3
  • 23
  • 47

2 Answers2

10

You cannot use BTC as the currency code for bitcoins under ISO 4217. BT is reserved for Bhutan. However, ISO 3166-1 reserves several country codes for user definition. Additionally, the wiki for ISO 4217 lists XBT as a currency code for bitcoins (unofficially, of course).

Locale.Builder b = new Locale.Builder();
b.setRegion("XB");
Locale xb = b.build();
Currency bitcoin = Currency.getInstance(xb);

Your currency.properties file will look like:

XB=XBT,000,3

Unfortunately, you cannot have 8 for the minor unit because the parsing for java.util.Currency only handles a minor unit of 0-3:

Pattern propertiesPattern = Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*([0-3])");
Jeffrey
  • 42,428
  • 7
  • 84
  • 138
  • Interesting, I will look into this. We're probably going to shelve it for now, but thanks for this! – tedski Dec 09 '13 at 16:39
  • Alright, I've tried your method but I can't seem to get it to pull from `currency.properties`. I even tried replacing Bhutan's currency just to see if it would work. Is there some cache I need to clear or something? – tedski Dec 13 '13 at 14:26
  • @tedski Not that I know of. Are you placing the file in the correct location? Try doing `System.println(System.getProperty("java.home"))` to see where `$JAVA_HOME` is. (You might have multiple JVMs installed on your computer or if you're using the JDK it might be using the `jre` folder as its home.) – Jeffrey Dec 13 '13 at 14:59
  • That was it, I had it in the wrong `/lib` folder. Ended up being `$JAVA_HOME/jre/lib`. Thanks! – tedski Dec 13 '13 at 15:48
  • Hey @Jeffrey , do you know if there's any way to pass the `XB=XBT,000,3` as a -D flag instead of that properties file? – tedski Jan 08 '14 at 14:39
  • @tedski I don't know. You could try making your own [`LocaleServiceProvider`](http://docs.oracle.com/javase/7/docs/api/java/util/spi/LocaleServiceProvider.html) and going from there, but I don't see anything about passing it as a D flag. – Jeffrey Jan 08 '14 at 16:01
  • Caveat: the first 2 chars of the currency code must be equal to country code – Roman Plášil Oct 18 '17 at 10:49
-3

How to handle bitcoin money is explained well on the documentation. Take a look at this https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)

Hope it helps

Abishek
  • 9,788
  • 17
  • 65
  • 106