PHP Reference ยท Math Functions
PHP BCMath functions
Reference for PHP BCMath: bcadd, bcsub, bcmul, bcdiv, bcmod, bcpow, bcsqrt, bccomp, scale handling, rounding in PHP 8.4 and the BcMath\Number class.
BCMath is an extension for arbitrary-precision decimal arithmetic. Numbers are passed and returned as strings, so they can have as many digits as memory allows and are free of binary floating-point error. This is the standard way to handle money, tax rates and other values that must add up exactly.
The key concept is scale: the number of digits kept after the decimal point. Every function accepts an optional $scale; if you leave it out, the default scale (0 unless set by bcscale() or the bcmath.scale ini setting) is used. Results are truncated, not rounded, to that scale. The extension must be enabled (extension_loaded("bcmath")).
<?php echo 0.1 + 0.2; // 0.3 (looks fine, but is 0.30000000000000004) echo bcadd("0.1", "0.2", 1); // 0.3, exactly
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
bcadd(string $num1, string $num2, ?int $scale = null): string bcsub(string $num1, string $num2, ?int $scale = null): string bcmul(string $num1, string $num2, ?int $scale = null): string bcdiv(string $num1, string $num2, ?int $scale = null): string bcmod(string $num1, string $num2, ?int $scale = null): string bcpow(string $num, string $exponent, ?int $scale = null): string bcsqrt(string $num, ?int $scale = null): string bccomp(string $num1, string $num2, ?int $scale = null): int bcscale(?int $scale = null): int bcpowmod(string $num, string $exponent, string $modulus, ?int $scale = null): string bcfloor(string $num): string // PHP 8.4 bcceil(string $num): string // PHP 8.4 bcround(string $num, int $precision = 0, RoundingMode $mode = RoundingMode::HalfAwayFromZero): string // PHP 8.4
| Name | Description |
|---|---|
$num1, $num2, $num |
Numbers as strings: optional sign, digits, optional decimal point. No exponent notation, no spaces. |
$scale |
Digits kept after the decimal point. null uses the default scale. |
$exponent |
For bcpow(), an integer-valued string (may be negative). |
$modulus |
For bcpowmod(), the divisor. |
Return value: a numeric string with exactly $scale decimals (zero decimals means no decimal point). bccomp() returns -1, 0 or 1, comparing only up to $scale digits. bcscale() returns the previous scale.
Errors: ValueError for a malformed number string or a negative scale; DivisionByZeroError for division or modulus by zero and for bcpowmod() with a zero modulus.
Usage notes
- Pass strings. Passing floats works, but the float's imprecise value is converted first, defeating the purpose. Keep money as strings from the database onward, or as integer cents.
- Truncation, not rounding.
bcdiv("2", "3", 2)is0.66. Compute with extra digits and round at the end (bcround()in PHP 8.4, or add half a unit as in the example). - Scale 0 by default makes
bcadd("1.9", "1.9")return3. Setbcscale()early or pass the scale each time. - Compare with
bccomp(), never with==on the strings ("1.0"and"1.00"differ as strings) and not with<on floats. - Speed: BCMath is orders of magnitude slower than native ints and floats. Use it for money and exactness, not numerical computing.
- PHP 8.4 additions:
bcfloor(),bcceil(),bcround(),bcdivmod()and the immutableBcMath\Numberclass, which supports the ordinary operators (+,-,*,/,**, comparison) on exact numbers. - Negative zero: since PHP 7.2 results never show
-0. - Not installed? On some systems it needs enabling; use
extension_loaded("bcmath"). For huge integers only, GMP is faster.