phpcurrency

How to get float currency value from Brick\Math\BigDecimal?


Having brick/money on laravel 11 site I make conversion of currencies like :

$provider->setExchangeRate($baseCurrencyCharCode, $currencyCharCode, $currencyTableRow->value );

$converter = new CurrencyConverter($provider);

$money = Money::of($amount, $baseCurrencyCharCode);
$value = $converter->convert($money, $currencyCharCode, roundingMode: RoundingMode::DOWN);
dd(   BigDecimal::of($value->getAmount())   );

But outputing value I got I see

Brick\Math\BigDecimal {#1678 ▼ // app/Library/Services/BrickMoneyConverter.php:61
    -value: "7340"
    -scale: 2
}

Valid result must be float 73.40. I did not find which method have I to use to get it ? I do not have to worry about scale of currency. Seems this library knows how to work with scales ?

"brick/money": "^0.9.0"
"laravel/framework": "^11.9",
php 8.2

Solution

  • To get the string representation of a BigDecimal, just cast it to a string.

    If you want to convert it to a float, call the toFloat() method.

    require 'vendor/autoload.php';
    
    use Brick\Money\Money;
    
    $money = Money::of('73.40', 'USD');
    $amount = $money->getAmount();
    
    printf('String value: %s<br>', (string)$amount);
    printf('Float value: %.2f<br>', $amount->toFloat());
    

    Output:

    String value: 73.40
    Float value: 73.40