Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of int to int (getAmount()) via ->convert() returning decimal of pence. #103

Open
jdfx opened this issue May 23, 2024 · 1 comment

Comments

@jdfx
Copy link

jdfx commented May 23, 2024

Unless I am doing something wrong, the following seems incorrect:

Custom conversion of 0.36 EUR to GBP using an exchange rate of 1.10 EUR to 1 GBP. To get the conversion ratio going EUR to GBP we do:

1 / 1.10 = 0.90909090909 (or 0.91 as it comes out).

Then the ->convert function is taking the ->amount (36 - cents) value of 0.36 (EUR).

And doing the following: 0.36 * 0.91 = 32.76 which is correct technically, except we are converting pence here so you cannot have 32.76 pence, it should round up to 33pence.

The ->round() method is checking for the precision of GBP via $this->currency->getPrecision() and returning it at 2 decimal places for GBP which would be correct if we were dealing with pounds, but we're not. It should be 0.

Returning getAmount(true) to force rounding is not working as it is doing the same check as above.

I am likely doing something incorrectly, any ideas or is this a bug?

@jdfx
Copy link
Author

jdfx commented May 24, 2024

If the converted amount result is less than the subunit, it breaks.
This fixes it but I don't know if somewhere else I have managed to cause an error, I don't think so.

    /**
     * @psalm-suppress ArgumentTypeCoercion
     */
    public function round(int|float $amount, int $mode = self::ROUND_HALF_UP): float
    {
        $this->assertRoundingMode($mode);
        $precision = ($amount < $this->currency->getSubunit()) ? 0 : $this->currency->getPrecision();
        return round($amount, $precision, $mode);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant