Closed jokaorgua closed 3 years ago
Hi, you have a few options with a MoneyBag
:
convert it to a Money using a CurrencyConverter
:
$exchangeRateProvider = new ConfigurableProvider();
// set up your exchange rates here...
$exchangeRateProvider->setExchangeRate('EUR', 'USD', '1.1');
$exchangeRateProvider->setExchangeRate('EUR', 'GBP', '0.9');
$converter = new CurrencyConverter($exchangeRateProvider);
$converter->convert($moneyBag, 'EUR', RoundingMode::DOWN);
$amount = $moneyBag->getAmount('EUR');
Note that this does not convert the the total amount (in all currencies) of the MoneyBag
to this currency, but only returns the value explicitly set in this currency.
Now both Money::getAmount()
and MoneyBag::getAmount()
return a BigNumber
(respectively a BigDecimal
and a BigRational
).
If you want to convert a BigNumber
that does not represent an integer value to an integer, you need to round it first:
use Brick\Math\RoundingMode;
$amount->toScale(0, RoundingMode::DOWN)->toInt();
You can also apply a rounding to 2 decimals, if you wish:
echo $amount->toScale(2, RoundingMode::DOWN); // e.g.: 12.34
Does that answer your question?
thank you @BenMorel
Seems to me too much things have to be done for using MoneyBag. As for me would be easier if MoneyBag returned Money object with currency, rouding, etc. When I add Money object I expect it to be returned, without hidden conversion to another object(BigRational).
Hi, the real purpose of MoneyBag
is not to store monies to be retrieved as is. Actually, it's not really meant to be used to retrieve anything directly. Its goal is to be used through a CurrencyConverter
to convert this bag of money to a single Money
in a single currency.
It stores monies internally as BigRational
because this is the lowest common denominator for all accepted money objects: Money
, RationalMoney
, MoneyBag
.
The real use case for a MoneyBag
is therefore, as mentioned above:
// want a Money?
$money = $converter->convert($moneyBag, 'EUR', RoundingMode::DOWN); // e.g. EUR 1.23
// want cents?
echo $money->getMinorAmount()->toInt(); // e.g. 123
I'm closing this issue, but feel free to comment if you need extra help.
Hello.
I just want to understand the correct way of working with MoneyBag.
First, we have an instance of Money class
Second, we create a money bag and add money to it
And when we are going to use that MoneyBag we have a BigRational number
and how to use this BigRational for normal numbers? Let's say I want to convert it to int, float, etc
I always get
And I don't quite understand where and how to add rounding in this case. Need an advice
Thank you.