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. Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】

    原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...

  2. Android 八款开源 Android 游戏引擎

    原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...

  3. thinkphp3错误:syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING)

    syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING) 出现这个错误的原因是,list是php的一个函数,系 ...

  4. Android 之 WebView开发问题及优化

    WebView 在现在的项目中使用的频率应该还是非常高的,WebView 主要用来加载一些容易改变的频繁交互的应用App.目前 HTML5 是一种趋势.在开发中会遇到一些开发问题及优化问题,如下所记. ...

  5. SpringBoot使用JSP渲染页面

    1.pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  6. error: expected unqualified-id extern "C" {

    通常为include该文件的头文件内类的声明处未加“:”

  7. oracle-sakila-db sakila的Oracle版本

    在Sakila——MySQL样例数据库解析 中,学习了sakila for mysql的表结构.并把它作为数据仓库.ODI ETL学习的源数据库.ODI Studio拓扑结构的创建与配置 后来发现OD ...

  8. 通过 Apache Commons HttpClient 发送 HTTPS 请求

    1.通过 HTTPS 发送 POST 请求: 2.HTTPS 安全协议采用 TLSv1.2: 3. 使用代理(Proxy)进行 HTTPS 访问: 4.指定 Content-Type 为:applic ...

  9. MySQL Gap Lock问题

    四种隔离级别说明 隔离级别 脏读(Dirty Read) 不可重复读(NonRepeatable Read) 幻读(Phantom Read) 未提交读(Read uncommitted) 可能 可能 ...

  10. Npoi导出excel整理(附源码)

    前些日子做了一个简单的winform程序,需要导出的功能,刚开始省事直接使用微软的组件,但是导出之后发现效率极其低下,绝对像web那样使用npoi组件,因此简单的进行了整理,包括直接根据DataTab ...