bccomp php扩展,PHP: bccomp - Manual

I made this to compare an unlimited size of numbers..

This could be useful for those without the BCMath extension.

It allows decimals, and option $Scale parameter.  If $Scale isn't specified, then it'll automatically adjust to the correct number of decimals to compare.

!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');// remove leading zeroes from whole numbers$Num1=ltrim($Tmp1[1],'0');$Num2=ltrim($Tmp2[1],'0');// first, we can just check the lengths of the numbers, this can help save processing time

// if $Num1 is longer than $Num2, return 1.. vice versa with the next step.if(strlen($Num1)>strlen($Num2)) return(1);

else {

if(strlen($Num1)

}// calculate the longest length of decimals$DLen=max(strlen($Dec1),strlen($Dec2));// append the padded decimals onto the end of the whole numbers$Num1.=str_pad($Dec1,$DLen,'0');$Num2.=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

if((int)$Num1{$i}>(int)$Num2{$i}) return(1);

else

if((int)$Num1{$i}

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

}

}

}$A="10.50002";$B="10.50001";printf("  Comp(%s,%s); // %s\r\n",$A,$B,Comp($A,$B));printf("BCComp(%s,%s); // %s\r\n",$A,$B,BCComp($A,$B));/*

Comp(10.50002,10.50001); // 1

BCComp(10.50002,10.50001); // 0 (BCComp has a default decimal scale of 0, unless specified)

*/?>

I tried to make this behave like BCComp..

The only difference being mine will compare the decimals by default.. BCComp won't..

.. unless, of course, you specify the amount of decimals to include in the process.

Enjoy,

Nitrogen.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值