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. Spark Structured Streaming:将数据落地按照数据字段进行分区方案

    方案一(使用ForeachWriter Sink方式): val query = wordCounts.writeStream.trigger(ProcessingTime(5.seconds)) . ...

  2. JPA(四):EntityManager

    Persistence Persistence类使用于获取EntityManagerFactory实例,该类包含一个名为createEntityManagerFactory的静态方法. // 创建En ...

  3. 浅谈压缩感知(十四):傅里叶矩阵与小波变换矩阵的MATLAB实现

    主要内容: 傅里叶矩阵及其MATLAB实现 小波变换矩阵及其MATLAB实现  傅里叶矩阵及其MATLAB实现 傅里叶矩阵的定义:(来源: http://mathworld.wolfram.com/F ...

  4. Discuz常见小问题-如何批量加精,置顶帖子

    批量选中帖子,然后置顶和精华都可以勾选 完成之后的效果

  5. JERSEY中文翻译(第三章、JAX-RS Application, Resources and Sub-Resources)

    JAX-RS Application Resource and Sub-Resource 本章要介绍的是JAX-RS的核心概念——Resouce.Sub-Resource JAX-RS的2.0的jav ...

  6. infobright系列二:数据迁移

    安装之后把之前infobright的数据迁移到新安装的infobright上. 1:挺掉相关的服务 2:scp 把旧数据拷到新安装的infobright上 3:修改/etc/my-ib.cnf的数据目 ...

  7. Struts2学习笔记四:深入拦截器

    一:拦截器的工作原理 拦截器的执行过程可以类比filter过滤器,ActionInvocation实例执行过程中,先执行action实例上引用的拦截器们,然后才执行action实例处理请求,返回res ...

  8. MyEclipse 本地安装插件

    安装subeclipse插件 打开之前下载的site-1.6.12.zip文件可以看到里面有features.plugins两个文件夹 用之前我讲过的Myeclipse安装插件的方法安装就可以了 参考 ...

  9. JVM中java实例对象在内存中的布局

    普通的Java对象实例 和  Java数组实例.Java数组实例的对象头多了一个数组的长度.Java虚拟机可以通过普通java对象的元数据来确定java对象的大小,但是从数组的元数据中却无法确定数组的 ...

  10. python 怎么模拟加header(如User-Agent、Content-Type等等)

    # -*- coding: cp936 -*- #python 27 #xiaodeng #python 怎么模拟加header(如User-Agent.Content-Type等等) #办法一: i ...