PHP amount calculation high-precision function

Precision function 

The result is 2222, not 0.8. Because there is an accuracy problem, the judgment is not equal, so the output is 2222

PHP浮点数计算精度不够,以下是几个精度计算函数
bcadd — 加法
bccomp — 比较
bcdiv — 相除
bcmod — 求余数
bcmul — 乘法
bcpow — 次方
bcpowmod — 先次方然后求余数
bcscale — 给所有函数设置小数位精度
bcsqrt — 求平方根
bcsub — 减法

Calculation

<?php

/**
 * PHP精确计算  主要用于货币的计算用
 * @param $n1 第一个数
 * @param $symbol 计算符号 + - * / %
 * @param $n2 第二个数
 * @param string $scale  精度 默认为小数点后两位
 * @return  string
 */
class Price{
   public static function pricecalc($n1, $symbol, $n2, $scale = '2')
    {
        $res = "";
        switch ($symbol) {
            case "+"://加法
                $res = bcadd($n1, $n2, $scale);
                break;
            case "-"://减法
                $res = bcsub($n1, $n2, $scale);
                break;
            case "*"://乘法
                $res = bcmul($n1, $n2, $scale);
                break;
            case "/"://除法
                $res = bcdiv($n1, $n2, $scale);
                break;
            case "%"://求余、取模
                $res = bcmod($n1, $n2, $scale);
                break;
            default:
                $res = "";
                break;
        }
        return $res;
    }
    /**
     * 价格由元转分(用于微信支付单位转换)
     * @param $price 金额
     * @return int
     */
    public static function priceyuantofen($price){
        $price = intval(self::pricecalc(100, "*",$price));
        return $price;
    }
    /**
     * 价格由分转元
     * @param $price 金额
     * @return float
     */
    public static function pricefentoyuan($price){
        $price = self::pricecalc(self::priceformat($price),"/",100);
        return $price;
    }
    /**
     * 价格格式化
     *
     * @param int $price
     * @return string    $price_format
     */
    public static function priceformat($price){
        $price_format = number_format($price, 2, '.', '');
        return $price_format;
    }
}

 

Guess you like

Origin blog.csdn.net/fujian9544/article/details/110196059