php bccomp的替换函数

简介: if (!function_exists('bccomp')) { /** * 支持正数和负数的比较 * ++ -- +- * @param $numOne * @param $numTwo * @param null $scale ...
if (!function_exists('bccomp')) {

    /**
     * 支持正数和负数的比较
     * ++ -- +-
     * @param $numOne
     * @param $numTwo
     * @param null $scale
     * @return int|string
     */
    function bccomp($numOne, $numTwo, $scale = null)
    {
        //先判断是传过来的两个变量是否合法,不合法都返回'0'
        if (!preg_match("/^([+-]?)\d+(\.\d+)?$/", $numOne, $numOneSign) ||
            !preg_match("/^([+-]?)\d+(\.\d+)?$/", $numTwo, $numTwoSign)
        ) {
            return '0';
        }

        $signOne = $numOneSign[1] === '-' ? '-' : '+';
        $signTwo = $numTwoSign[1] === '-' ? '-' : '+';

        if ($signOne !== $signTwo) {    //异号
            if ($signOne === '-' && $signTwo === '+') {
                return -1;
            } else if ($signOne === '+' && $signTwo === '-') {
                return 1;
            } else {
                return '0';
            }
        } else {  //同号
            //两个负数比较
            if ($signOne === "-" && $signTwo === '-') {
                $numOne = abs($numOne);
                $numTwo = abs($numTwo);
                $flag = bccompPositiveNum($numOne, $numTwo, $scale);
                if ($flag === 0) {
                    return 0;
                } else if ($flag === 1) {
                    return -1;
                } else if ($flag === -1) {
                    return 1;
                } else {
                    return '0';
                }
            } else {    //两个正数比较
                //两正数比较
                return bccompPositiveNum($numOne, $numTwo, $scale);
            }
        }
    }
}


if (!function_exists('bccompPositiveNum')) {
    /**
     * 比较正数的大小写问题
     * @param $numOne
     * @param $numTwo
     * @param null $scale
     * @return int|string
     */
    function bccompPositiveNum($numOne, $numTwo, $scale = null)
    {
        // check if they're valid positive numbers, extract the whole numbers and decimals
        if (!preg_match("/^\+?(\d+)(\.\d+)?$/", $numOne, $tmpOne) ||
            !preg_match("/^\+?(\d+)(\.\d+)?$/", $numTwo, $tmpTwo)
        ) {
            return '0';
        }

        // remove leading zeroes from whole numbers
        $numOne = ltrim($tmpOne[1], '0');
        $numTwo = ltrim($tmpTwo[1], '0');

        // first, we can just check the lengths of the numbers, this can help save processing time
        // if $numOne is longer than $numTwo, return 1.. vice versa with the next step.
        if (strlen($numOne) > strlen($numTwo)) {
            return 1;
        } else {
            if (strlen($numOne) < strlen($numTwo)) {
                return -1;
            } // if the two numbers are of equal length, we check digit-by-digit
            else {

                // remove ending zeroes from decimals and remove point
                $Dec1 = isset($tmpOne[2]) ? rtrim(substr($tmpOne[2], 1), '0') : '';
                $Dec2 = isset($tmpTwo[2]) ? rtrim(substr($tmpTwo[2], 1), '0') : '';

                // if the user defined $scale, then make sure we use that only
                if ($scale != null) {
                    $Dec1 = substr($Dec1, 0, $scale);
                    $Dec2 = substr($Dec2, 0, $scale);
                }

                // calculate the longest length of decimals
                $DLen = max(strlen($Dec1), strlen($Dec2));

                // append the padded decimals onto the end of the whole numbers
                $numOne .= str_pad($Dec1, $DLen, '0');
                $numTwo .= str_pad($Dec2, $DLen, '0');

                // check digit-by-digit, if they have a difference, return 1 or -1 (greater/lower than)
                for ($i = 0; $i < strlen($numOne); ++$i) {
                    if ((int)$numOne{$i} > (int)$numTwo{$i}) {
                        return 1;
                    } elseif ((int)$numOne{$i} < (int)$numTwo{$i}) {
                        return -1;
                    }
                }

                // if the two numbers have no difference (they're the same).. return 0
                return 0;
            }
        }
    }
}

 

目录
相关文章
|
5月前
|
Shell PHP Windows
PHP代码审计(四)PHP文件操作函数(2)
改变文件所有者。如果成功则返回 TRUE,如果失败则返回 FALSE。 语法:chown(file,owner)
38 0
|
5月前
|
安全 Unix Shell
PHP代码审计(四)PHP文件操作函数(1)
改变文件所有者。如果成功则返回 TRUE,如果失败则返回 FALSE。 语法:chown(file,owner)
38 0
|
5月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
47 0
|
8月前
|
PHP
PHP 常用系统函数
PHP 常用系统函数
39 0
|
4天前
|
PHP Perl
PHP-ereg()函数
但再次强调,不建议使用ereg()函数,而应该改用preg_match()函数来执行更灵活和强大的正则表达式匹配操作。
16 4
|
8天前
|
存储 PHP 数据库
【PHP开发专栏】PHP数组操作与常见函数
【4月更文挑战第29天】本文介绍了PHP中的数组及其操作,包括定义与初始化、访问与修改、遍历。还探讨了常用的数组函数,如count()、in_array()、array_push/pop()、array_shift/unshift()、array_merge()、array_keys/values()以及sort()和rsort(),帮助开发者更有效地处理和操作数组。
|
9天前
|
运维 JavaScript Serverless
Serverless 应用引擎产品使用之在阿里函数计算中,Php环境,配置取消禁止函数exec如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
28 4
|
28天前
|
JSON PHP 数据格式
php函数
php函数
7 0
|
1月前
|
PHP
php事务删除加调用日志函数
php事务删除加调用日志函数
7 1
|
3月前
|
PHP
从建站到拿站 -- PHP判断循环及函数
从建站到拿站 -- PHP判断循环及函数
14 0