zoukankan      html  css  js  c++  java
  • 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;
        }
    
    
    }
  • 相关阅读:
    Autho2----完整搭建实例
    详解SpringBoot应用跨域访问解决方案
    微信小程序后端开发流程
    前端必备 Nginx 配置
    后端必备 Nginx 配置
    关于spring boot集成MQTT
    Java 常用IO流操作详解
    spring boot 整合mybatis 的xml版本【包括逆向工程以及分页插件】
    实用 SQL 语句
    整理收集的一些常用java工具类
  • 原文地址:https://www.cnblogs.com/zbseoag/p/14888910.html
Copyright © 2011-2022 走看看