I am making a Currency conversion app in Java. Some other awesome StackOverflowians gave me advice to read up on BigDecimal for the purpose of replacing double to fix any precision issues.
I have a two method system; where it converts from the starting currency into USD, then converts the USD value to the destination currency.
Note, my conversion rates are stored like this:
// Conversion Rates - START (as of October 30, 2018 @ 3:19 AM)
// Rates obtained from exchange-rates.org
//Convert to United States Dollar rates
private final BigDecimal CAD_TO_USD = new BigDecimal(0.76135);
private final BigDecimal EUR_TO_USD = new BigDecimal(1.1345);
private final BigDecimal YEN_TO_USD = new BigDecimal(0.008853);
// Conversion Rates - END
After I replaced my doubles with their respective BigDecimals - I decided to test it and see how it all works out.
My tester class runs the following method to start the conversion process.
public BigDecimal convert()
{
BigDecimal value;
value = convertToUSD(); //Converts the current currency into USD
value = convertFromUSD(value); //Converts the previous USD currency value into the destination currency
return value;
}
When I enter my sample variables (which are converting 2.78 YEN to Canadian Dollars), I stepped through the process and found that everything functions up until I return a value.
From the earlier mentioned method, convertToUSD() is run and is coded as follows
private BigDecimal convertToUSD()
{
switch (fromCurrency)
{
case "USD":
return fromQuantity.multiply(new BigDecimal(1));
case "CAD":
return fromQuantity.multiply(CAD_TO_USD);
case "EUR":
return fromQuantity.multiply(EUR_TO_USD);
case "YEN":
return fromQuantity.multiply(YEN_TO_USD);
}
return new BigDecimal(0);
}
ALl the values are passed in correctly, it steps through down to the proper case ("YEN"), and the variable pane shows that the "fromQuantity" BigDecimal has a intCompact value of 278 (which makes sense to me)
As soon as the breakpoint returns back to the "convert" method, it gets all messed up. Instead of returning the 2.78 * 0.008853 = 0.0246, it returns -9223372036854775808.
This causes all other calculations to produce and error.
I am new to using BigDecimal, so I may be making a complete obvious mistake; but I am happy to learn, so I sought advice from you guys :)
Any assistance is appreciated.


