MinorUnitsConverter.php in Commerce Core 8.2
File
modules/price/src/MinorUnitsConverter.php
View source
<?php
namespace Drupal\commerce_price;
use CommerceGuys\Intl\Currency\CurrencyRepositoryInterface;
class MinorUnitsConverter implements MinorUnitsConverterInterface {
protected $currencyRepository;
public function __construct(CurrencyRepositoryInterface $currency_repository) {
$this->currencyRepository = $currency_repository;
}
public function fromMinorUnits($amount, $currency_code) : Price {
$fraction_digits = $this->currencyRepository
->getDefaultFractionDigits($currency_code);
if ($fraction_digits > 0) {
$amount = Calculator::divide((string) $amount, pow(10, $fraction_digits), $fraction_digits);
}
return new Price((string) $amount, $currency_code);
}
public function toMinorUnits(Price $amount) : int {
$fraction_digits = $this->currencyRepository
->getDefaultFractionDigits($amount
->getCurrencyCode());
$number = $amount
->getNumber();
if ($fraction_digits > 0) {
$number = Calculator::multiply($number, pow(10, $fraction_digits));
}
return round($number);
}
}