amrshawky / laravel-currency

Laravel package for current and historical currency exchange rates & crypto exchange rates. based on the free API exchangerate.host
MIT License
335 stars 44 forks source link

Converted amount is being rounded up. #11

Closed chard1988 closed 1 year ago

chard1988 commented 2 years ago

I used this package for a project and noticed that the converted amount is being rounded up.

image

image

`function monetize($amount) {

    // Get current location
    $ip = request()->ip();
    $geoData = geoip($ip);

    // Get currency symbol
    $dbCurrency = Currency::where('code', $geoData['currency'])->first();

    // Strip "," symbol
    $stripped = str_replace(',', '', number_format($amount/100, 2));

    if ($amount != 0) {
        $retval = CurrencyConverter::convert()
            ->from('PHP')
            ->to($geoData['currency'])
            ->amount($stripped)
            ->round(2)
            ->get();
    } else {
        $retval = number_format($amount/100, 2);
    }

    $retval =  $dbCurrency->symbol . ' ' . $retval;
    $decimal = substr($retval, strpos($retval, '.') + 1);
    $cur = strtok($retval, '.');

    return __(':cur<small>.:decimal</small>', ['cur' => $cur, 'decimal' => $decimal]);
}`

is there any way to prevent this from happening?

Kind regards,

amrshawky commented 1 year ago

Remove the ->round(2) from your call