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. linux kernel内存映射实例分析

    作者:JHJ(jianghuijun211@gmail.com)日期:2012/08/24 欢迎转载,请注明出处 引子 现在android智能手机市场异常火热,硬件升级非常迅猛,arm cortex ...

  2. linux ps 命令的查看

    linux ps 命令的结果中VSZ,RSS,STAT的含义和大小 ps是linux系统的进程管理工具,相当于windows中的资源管理器的一部分功能. 一般来说,ps aux命令执行结果的几个列的信 ...

  3. 为什么有的需要安全连接的的application只有开Fiddler才好用?

      Help! Running Fiddler Fixes My App??? Over the years, the most interesting class of support reques ...

  4. C# 源码 AForge.NET

    AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域.这个框架由一系列的类库组成.主要包括有 ...

  5. [Javascript] Check both prop exists and value is valid

    Sometime you need to check one prop exists on the object and value should not be ´null´ or ´undefine ...

  6. Android 高级 Jackson Marshalling(serialize)/Unmarshalling(deserialize)

    本文内容 高级 Jackson Marshalling 只序列化符合自定义标准的字段 把 Enums 序列化成 JSON 对象 JsonMappingException(没有找到类的序列化器) Jac ...

  7. 【iOS开发】如何用 Swift 语言进行LBS应用的开发?

    本文分为三部分,第一部分详解用Swift语言开发LBS应用,并给出完整的示例与源代码:第二部分介绍如何申请LBS密钥,第三部分是综合示例查看,扫描二维码即可查看示例demo. 第一部分 使用Swift ...

  8. 【转】Ant之build.xml详解

    关键字: ant build.xml Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序 ...

  9. mac pro 显示隐藏文件

    经常希望在IOS操作系统现实隐藏文件,下面两条语句可以使用: 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:de ...

  10. 动态加载jar包(一)

    一.编写被调用的类 package com.qunar.helloworld; public class HelloWorld { public String sayHello(){ return ( ...