Search code examples
javaprecisioncurrency

Currency Converter in Java having precision and condensing issues


I have gotten an assignment that is to be written in Java.

We are to develop a currency conversion application which asks the currency they are converting from, the amount of said currency, and the currency they wish to convert to.

The instruction noted we should include 4 currencies to convert from/to.

I got the assignment done early and I went to show my professor and she noted a couple of issues with it. She likes the organisation and the clarity of the code, but she thinks I can make it a bit smaller and fix an issue I have with decimal precision.

In regards to the making of it shorter, her main argument was that I have 7 constants which hold the exchange rates. I was rather proud of that since 7 rates is alot shorter than 12 individual rates for every possible combination. Code is below.

// Conversion Rates - START (as of October 14, 2018 @ 1:03 AM)
// Rates obtained from exchange-rates.org
private final double USD_TO_USD = 1;

//Convert to United States Dollars
private final double CAD_TO_USD = 0.76792;
private final double EUR_TO_USD = 1.1407;
private final double YEN_TO_USD = 0.008923;

//Convert from United States Dollars
private final double USD_TO_CAD = 1.3022;
private final double USD_TO_EUR = 0.87662;
private final double USD_TO_YEN = 112.06;
// Conversion Rates - END

My thought process behind my methodology is to convert everything to US Dollars, then convert from US Dollars to the destination currency. I am unsure of how to condense this any further, so that is the one of the issues I am having.

The main issue I am coming across is precision. Since the class is largely based on business mathematics, the programs are generally used to convert millions/billions of currency. I know that double already has its own inherit issues with precision, but I am unsure of what other data types to use. I have come across BigDecimal, and I will take a look into that after posting this.

To my understanding, the number of decimal points in the exchange rate will directly affect the precision of the result. the more numbers to the right of the rate; the better. Should I make a habit of including a very large amount of decimal points to make it very difficult to have the precision issue arise? Or would the use of BigDecimal usually solve the issue.

I have considered dropping the use of double and use int instead; so instead of a double holding 5.00, I would have an integer holding 500; but at this point, I am unsure of the "proper" way to proceed.

So I came to ask you fine people :D

I am happy to learn as much as I can, so any assistance is appreciated.

Thanks

UPDATE: I took some time to check out BigDecimal, and I got it working; except I am now off by an amount ranging from 1 to 10 (might be more in very very large numbers, but I didn't test it more than 5 times with different numbers).

In my testing, I converted 98765432.00 Japanese Yen to US Dollars, with a rate of 1 YEN = 0.008907 US Dollars. According to the website I used to verify at the time - the result in US Dollars should be USD$879,701.24; but I am getting $879,703.70 on my program. Even my scientific calculator is getting the same thing that my java program is getting.


Solution

  • Just go ahead with your approach of implementing with BigDecimal as with BigDecimal you won't lose any precision but with double there is a chance of losing when you are dealing with larger numbers.

    Please go through the below stackoverflow link to get more idea on BigDecimal: Double vs. BigDecimal?

    You are on the right track, keep rocking and happy learning.