We’re using Ruby’s Money gem. In our application, we’re also converting between currencies.
So the issue of rounding comes up when you convert between currencies. Currently we’re using the Ruby’s built-in Float#round.
However, this introduces “the dangling penny” problem.
Money.new(500,'USD').exchange_to('EUR')
#=> #Money cents:385 currency:EUR
Money.new(500,'USD').exchange_to('EUR').exchange_to('USD')
#=> #Money cents:501 currency:USD
So $5.00 becomes $5.01 after converting to Euro and then back! These are the exchange rates we’re using:
{
"GBP_TO_USD"=>1.6,
"USD_TO_GBP"=>0.625,
"GBP_TO_EUR"=>1.2,
"EUR_TO_GBP"=>0.8333333333333334,
"EUR_TO_USD"=>1.3,
"USD_TO_EUR"=>0.7692307692307692
}
Is it possible to have a rounding method to avoid this problem of an extra or missing penny? What would that rounding method be?
Or is this a mathematically insoluble problem?
Once you round a number, the information is lost. The best solution to your problem is to stop rounding (which you should not do with money anyway) and only round when you are displaying the value somewhere.