Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (46 sloc) 1.45 KB
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\helpers;
use craft\commerce\models\PaymentCurrency;
use craft\commerce\Plugin;
/**
* Class Currency
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 2.0
*/
class Currency
{
// Public Methods
// =========================================================================
/**
* Rounds the amount as per the currency minor unit information. Not passing
* a currency model results in rounding in default currency.
*
* @param float $amount
* @param PaymentCurrency|null $currency
* @return float
*/
public static function round($amount, $currency = null): float
{
if (!$currency) {
$defaultPaymentCurrency = Plugin::getInstance()->getPaymentCurrencies()->getPrimaryPaymentCurrency();
$currency = Plugin::getInstance()->getCurrencies()->getCurrencyByIso($defaultPaymentCurrency->iso);
}
$decimals = $currency->minorUnit;
return round($amount, $decimals);
}
/**
* @return int
*/
public static function defaultDecimals(): int
{
$currency = Plugin::getInstance()->getPaymentCurrencies()->getPrimaryPaymentCurrencyIso();
$decimals = Plugin::getInstance()->getCurrencies()->getCurrencyByIso($currency)->minorUnit;
return $decimals;
}
}
You can’t perform that action at this time.