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

NumberFormatException on multiply operation #85

Closed
tems99 opened this issue Mar 7, 2024 · 2 comments
Closed

NumberFormatException on multiply operation #85

tems99 opened this issue Mar 7, 2024 · 2 comments

Comments

@tems99
Copy link

tems99 commented Mar 7, 2024

Version: 0.9.0
I ran a phpunit test on the following code.

$money1 = Money::of(100, 'USD');
$money2 = Money::of(200, 'USD');
$money3 = $money1->multipliedBy($money2);

The above code throws an exception.

Brick\Math\Exception\NumberFormatException: The given value "USD 200.00" does not represent a valid number.
/app/vendor/brick/math/src/Exception/NumberFormatException.php:14
/app/vendor/brick/math/src/BigNumber.php:118
/app/vendor/brick/math/src/BigNumber.php:61
/app/vendor/brick/math/src/BigRational.php:233
/app/vendor/brick/money/src/Money.php:405

Any ideas or am I doing something wrong?

@tems99
Copy link
Author

tems99 commented Mar 7, 2024

I read the documentation and it did not state that you could multiply with Money objects.
Also, the signature for the multiplyBy function does not contain Money or AbstractMoney as its parameters.
When debugging, if you pass in a Money object, it is accepted by the function as a string representation, which causes the error.
I will be closing this because it is not an issue.

@tems99 tems99 closed this as completed Mar 7, 2024
@BenMorel
Copy link
Member

BenMorel commented Mar 9, 2024

Hi, indeed, multiplying a Money with a Money does not make sense, as you found out, you need to multiply with a number!

I advise you to use strict_types, which would have caught the error early instead of attempting to cast the Money to string and use that as a number:

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Brick\Money\Money;

$money1 = Money::of(100, 'USD');
$money2 = Money::of(200, 'USD');
$money3 = $money1->multipliedBy($money2);

PHP Fatal error: Uncaught TypeError: Brick\Money\Money::multipliedBy(): Argument #1 ($that) must be of type Brick\Math\BigNumber|string|int|float, Brick\Money\Money given

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

2 participants