I know, there are might be lots of useful things missed in Java, let me talk today about one of them I faced recently. Well, honestly speaking it is not pure Java issue, there is more issue with the currency standardization process, but let’s start from the beginning.
Some time ago I was working with some currency processing logic using java.util.Currency class from Oracle JDK 10. The main logic was placed around processing fraction digit for different currencies. Obviously, that different currencies may have a different number of fraction digits. So, for example, Euro has 2, US Dollar has 2, but Japanise Yen has 0.
Using functionality of java.util.Currency class you can get a number of fraction digits in one line:
Very simple and clear code. Isn’t it?
We are moving ahead and I have one small question to you. I’m pretty sure that everybody (almost) in today’s world heard something about digital currencies (cryptocurrencies) itself and most popular of them Bitcoin. So, do you know what is a default fraction digits number for Bitcoin?
Yeah, it’s not so easy, but let’s try a verified approach with java.util.Currency usage:
Currency.getInstance("BTC").getDefaultFractionDigits() Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.util.Currency.getInstance(Currency.java:329) at java.base/java.util.Currency.getInstance(Currency.java:287)
Oh, it doesn’t work now. Why?
The java.util.Currency class represents different currencies around the globe based on ISO4217. The digital currencies are not a part of this standard yet, even if they look like standard. Moreover, the alphabetic codes used in ISO4217 are based on another ISO standard, ISO3166, which lists the codes for country names.
The Euro currency is the exceptional case related to the European Union which is not country and not formally included in ISO3166.
So, in case of digital currencies, there is no any relation to the country or geographical region. It’s around the globe. Hopefully, standardization is going ahead and digital currencies will be included in ISO4217 soon. That is a real example when in the modern world one technology is ahead of standards and other related technologies. There are different digital currency exchanges exist now, but no default instrument for the developer to work with the digital currencies in Java.
P.S. If you are still interested in the answer to the question what is a default fraction digits number for Bitcoin, the answer will be 8.