Torann / laravel-currency

This provides Laravel with currency functions such as currency formatting and conversion using up-to-date exchange rates.
http://lyften.com/projects/laravel-currency
BSD 2-Clause "Simplified" License
390 stars 137 forks source link

Precision error #81

Closed rajaneeshdwivedi closed 6 years ago

rajaneeshdwivedi commented 6 years ago

Great package. One suggestion is in convert(), in src\currency.php, to replace:

$value = $amount * $to_rate * (1 / $from_rate);

with

if ($from == $to)
    $value = $amount;
else
    $value = ($amount * $to_rate) / $from_rate;

as this solves a precision error where, in one case, a conversion of -2800 comes back as -2799 when blindly converting back to the same currency. Also the second line has greater precision and actually also solves my particular -2799 case even without the from-to check.

Torann commented 6 years ago

Just implement your fix. Thanks :)