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;
}
}
}
}

php bccomp的替换函数的更多相关文章

  1. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  2. PHP模板引擎正则替换函数 preg_replace 与 preg_replace_callback 使用总结

    在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换). 详情介绍参考博文:P ...

  3. php中几个字符串替换函数详解

    在php中字符替换函数有几个如有:str_replace.substr_replace.preg_replace.preg_split.str_split等函数,下面我来给大家总结介绍介绍. 一.st ...

  4. JS字符串替换函数:Replace(“字符串1″, “字符串2″),

    JS字符串替换函数:Replace(“字符串1″, “字符串2″), 1.我们都知道JS中字符串替换函数是Replace(“字符串1″, “字符串2″),但是这个函数只能将第一次出现的字符串1替换掉, ...

  5. strtr和str_replace字符替换函数

    (一)strtr是字符替换函数 (1)单个字符替换: <?php echo strtr("abba", "ab", "10"),&qu ...

  6. php中替换函数主要用的几个函数strtr(),str_repalce()。

    php中替换函数主要有strtr(),str_repalce()这两个函数,今天介绍下他们的区别和用法, 先来看看这个php字符串替换函数 strtr()的两种用法: strtr(string,fro ...

  7. php 函数strtr 替换函数实例解析 strtr 速度比较快

    先来看看这个php字符串替换函数 PHP字符串替换函数strtr()的两种状态 strtr(string,from,to) 或者strtr(string,array) 首先针对PHP字符串替换函数st ...

  8. JavaScript字符串插入、删除、替换函数

    JavaScript字符串插入.删除.替换函数 说明: 以下函数中前两个函数取出查找字符串的前一部分和后一部分,以用于其他函数.注意,调用一次 replaceString(mainStr,search ...

  9. Oracle的字符替换函数translate用法

    参考文档如下:http://www.banping.com/2009/05/18/oracle_function_translate/ Oracle提供了一个字符替换函数translate,不同于re ...

随机推荐

  1. Pearson(皮尔逊)相关系数

    Pearson(皮尔逊)相关系数:也叫pearson积差相关系数.衡量两个连续变量之间的线性相关程度. 当两个变量都是正态连续变量,而且两者之间呈线性关系时,表现这两个变量之间相关程度用积差相关系数, ...

  2. SqlServer驱动包 Maven

    SqlServer驱动包 Maven 学习了:https://blog.csdn.net/wu843820873/article/details/50484623 mvn install: mvn i ...

  3. Uniform and Interpolator Packing的作用

    All of the packing that is done is completely transparent to the user of the OpenGL ES Shading Langu ...

  4. JavaScript筛选出数组种连续的数字

    function arrange(source) { var t; var ta; var r = []; for(var j=0;j<source.length;j++){ var v=sou ...

  5. 一起talk C栗子吧(第一百三十三回:C语言实例--创建进程时的内存细节)

    各位看官们.大家好,上一回中咱们说的是从内存角度看进程和线程的样例.这一回咱们说的样例是:创建进程时的内存细节.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们.我们都知道使用fork函数能 ...

  6. brew install Jenkins

    Chens-MacBook-Pro:Downloads chenqing$ brew install jenkins ==> Downloading http://mirrors.jenkins ...

  7. JavaScript如何根据当天算出前三天和后三天

    经杨秀徐批准 中央军委颁发意见建设新型司令机关news 杨秀徐会见到北京述职的香港特首梁振英news 海军372潜艇官兵先进事迹报告会举行 杨秀徐作指示news 中央农村工作会议在京召开 李克强作重要 ...

  8. javascript高级语法学习

    可维护的代码意味着: 可读的 一致的 可预测的 看上去就像是同一个人写的 已记录 命名函数表达式 (function fn(){}) 他是表达式的原因是因为括号 ()是一个分组操作符,它的内部只能包含 ...

  9. input[type="checkbox"]与label对齐

    项目中遇到文字与 checkbook 无法水平对齐, 源码如下: <div align='center'> <input type="checkbox" id=& ...

  10. (转)pip和easy_install使用方式

    easy_install 跟 pip 都是 Python 的套件管理程式,有了它們,在使用 Python 開發程式的時候會帶來不少方便. easy_install 和 pip 有什麼不一樣?據 pip ...