37 lines
865 B
PHP
37 lines
865 B
PHP
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\service\util;
|
|
|
|
use app\service\ConfigService;
|
|
use function GuzzleHttp\Psr7\str;
|
|
|
|
/**
|
|
* @author canny
|
|
* @date 2023/10/20 15:28
|
|
**/
|
|
class PriceCalculatorService
|
|
{
|
|
public static function add($a, $b, $scale = 2): string
|
|
{
|
|
return bcadd((string)$a, (string)$b, $scale); // 保留两位小数
|
|
}
|
|
|
|
public static function subtract($a, $b, $scale = 2): string
|
|
{
|
|
return bcsub((string)$a, (string)$b, $scale);
|
|
}
|
|
|
|
public static function multiply($a, $b, $scale = 2): string
|
|
{
|
|
return bcmul((string)$a, (string)$b, $scale);
|
|
}
|
|
|
|
public static function divide($a, $b, $scale = 2): ?string
|
|
{
|
|
if (bccomp((string)$b, '0', 2) === 0) {
|
|
throw new \Exception('不允许除以零');
|
|
}
|
|
return bcdiv((string)$a, (string)$b, $scale);
|
|
}
|
|
} |