php BCmath 封装类

<?php
/**
 * BCmath 封装类
 * Calc::add(1,2,3)->sub(1,2)->mul(4, 5)->value(4); //按步骤计算1+2+3-1-2*4*5 最后调用 value 方法获取保留4位小数
 */
namespace AppTools;

/**
 * @method AppToolsCalc add(string ...$value) 加法
 * @method AppToolsCalc sub(string ...$value) 减法
 * @method AppToolsCalc mul(string ...$value) 乘法
 * @method AppToolsCalc div(string ...$value) 除法
 * @method AppToolsCalc comp(string ...$value) 比较
 * @method AppToolsCalc mod(string ...$value) 取模
 * @method AppToolsCalc pow(string ...$value) 乘方
 * @method AppToolsCalc sqrt(string ...$value) 开方
 */

class Calc
{
    protected $init = 0;
    protected $carry = [];

    public function __construct($value = 0)
    {
        $this->init = $value;
    }

    public static function init($value = 0)
    {
        return new static($value);
    }

    public static function __callStatic($method, $args)
    {
        return self::init(array_shift($args))->$method(...$args);
    }

    public function __call($method, $args)
    {
        $this->carry[] = ['bc' . $method => $args];
        return $this;
    }

    /**
     * 获取值
     * @param int $scale 保留小数位
     * @return string
     */
    public function value(int $scale = 2): string
    {
        foreach ($this->carry as $item) {
            foreach ($item as $func => $value) {
                $this->init = array_reduce($value, function ($carry, $val) use ($func, $scale) {
                    return $func($carry, $val, $scale);
                }, $this->init);
            }
        }
        return $this->init;
    }


}
原文地址:https://www.cnblogs.com/zbseoag/p/14888910.html