首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP BCMath 高精度函數庫完整參考手冊

     編輯:PHP 相關知識

PHP BCMath 高精度函數庫完整參考手冊

BCMath (Binary Calculator) 是 PHP 處理任意精度數學運算的標準擴展,廣泛應用於金融、電商、計費等對精度零容忍場景。本文整理所有 BCMath 函數的原型、參數說明、返回值與實戰代碼片段,作為開發查閱手冊。

函數總覽表

函數功能簽名關鍵參數
bcadd加法string bcadd(string $left, string $right, int $scale=0)$scale: 結果小數位數
bcsub減法string bcsub(string $left, string $right, int $scale=0)$scale: 結果小數位數
bcmul乘法string bcmul(string $left, string $right, int $scale=0)$scale: 結果小數位數
bcdiv除法string bcdiv(string $left, string $right, int $scale=0)$scale: 結果小數位數 (默認 0 即整除)
bcmod取模string bcmod(string $left, string $modulus)無 scale,整數運算
bcpow冪運算string bcpow(string $left, string $right, int $scale=0)$right 為指數,$scale 為結果小數位
bcpowmod冪取模string bcpowmod(string $left, string $right, string $mod, int $scale=0)數論核心運算 (RSA 加密常用)
bcsqrt平方根string bcsqrt(string $operand, int $scale=0)$scale: 結果小數位數
bccomp比較int bccomp(string $left, string $right, int $scale=0)返回 -1/0/1,$scale 設定比較精度
bcscale設定默認 scalebool bcscale(int $scale)影響後續不指定 scale 的函數

實戰代碼片段集

1. 比較兩個高精度數 (解決 0.00 != 0 問題)

<?php
$a = '4.45'; $b = '5.54';
if (bccomp($a, $b, 2) === 0) {
    echo "相等";
} elseif (bccomp($a, $b, 2) < 0) {
    echo "a < b";
} else {
    echo "a > b";
}
?>

2. 高精度加法

<?php
$a = '1.0321456'; $b = '0.0123456';
$c = bcadd($a, $b, 2); // '1.04'
?>

3. 高精度減法

<?php
$a = '1.0321456'; $b = '3.0123456';
$c = bcsub($a, $b, 2); // '-1.98'
?>

4. 取模運算

<?php
$a = '6'; $b = '4';
$c = bcmod($a, $b); // '2'
?>

5. 高精度除法 (注意 scale 必須顯式指定)

<?php
$a = '6'; $b = '5';
$c = bcdiv($a, $b, 3); // '1.200' (scale=3 保留3位)
?>

6. 高精度乘法

<?php
$a = '3.1415926'; $b = '2.4569874566';
$c = bcmul($a, $b, 6); // '7.718...'
?>

7. 冪運算

<?php
$a = '3.1415926'; $b = '2';
$c = bcpow($a, $b, 3); // '9.870'
?>

8. 平方根

<?php
$a = '2';
$b = bcsqrt($a, 6); // '1.414214'
?>

9. 設定全局默認小數位

<?php
bcscale(4); // 後續 bcadd/bcsub... 不寫 scale 時默認 4 位
echo bcadd('1.1', '2.2'); // '3.3000'
?>

避坑指南

  1. 參數必須為字符串:傳入 float 會觸發隱式轉換,引入精度誤差。永遠用 '1.23' 而非 1.23。
  2. bcdiv 默認 scale=0:不指定 scale 會整除,務必顯式傳入 scale。
  3. 負數支援:字符串可帶 - 符號,如 bcsub('1', '5', 2) → '-4.00'。
  4. 科學計數法不支援:'1.2E3' 會報錯,需預處理轉為普通十進制字符串。
  5. 性能提示:大量運算時,考慮 GMP 擴展 (資源型別,底層 C 優化) 或預計算緩存。

封裝建議:Money 价值對象

<?php
class Money {
    private $amount; // string
    private $currency;
    const SCALE = 4;

    public function __construct(string $amount, string $currency = 'CNY') {
        $this->amount = $amount;
        $this->currency = $currency;
    }

    public function add(Money $other): Money {
        if ($this->currency !== $other->currency) throw new Exception('Currency mismatch');
        return new Money(bcadd($this->amount, $other->amount, self::SCALE), $this->currency);
    }
    
    public function mul(string $factor): Money {
        return new Money(bcmul($this->amount, $factor, self::SCALE), $this->currency);
    }
    
    public function format(int $displayScale = 2): string {
        return bcadd($this->amount, '0', $displayScale); // 四捨五入格式化
    }
}
?>

將 BCMath 封裝為領域模型,讓業務代碼遠離底層精度細節,是成熟系統的必經之路。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved