PHP – How to divide two arbitrary precision numbers using bcdiv() function?

In PHP, bcdiv() math function is used to divide one arbitrary precision number from another number. The bcdiv() function takes two arbitrary precision numbers as strings and returns the result as a division of two numbers after scaling the result to a specified precision.

Syntax

string bcdiv($num_string1, $num_string2, $scaleVal)

Parameters

The bcdiv() function accepts three parameters:

  • $num_string1 − The dividend (string type parameter)

  • $num_string2 − The divisor (string type parameter)

  • $scaleVal − Optional integer parameter that sets the number of digits after the decimal point in the result. Default is 0

Return Value

The bcdiv() function returns the division result of $num_string1 divided by $num_string2 as a string.

Example 1 − Basic Division Without Scale

Here's a simple division without specifying decimal precision ?

<?php
    // PHP program to illustrate bcdiv() function
    // two input numbers using arbitrary precision
    $num_string1 = "22.5552"; // Dividend
    $num_string2 = "5";       // Divisor
    $result = bcdiv($num_string1, $num_string2);
    echo "Output without using Scale Value: " . $result;
?>
Output without using Scale Value: 4

Example 2 − Division With Scale Parameter

Now let's use the same input values with a scale value of 4 to get more precise results ?

<?php
    // PHP program to illustrate bcdiv() function with scale
    $num_string1 = "22.5552"; // Dividend
    $num_string2 = "5";       // Divisor
    
    // using scale value 4
    $scaleVal = 4;
    
    $result = bcdiv($num_string1, $num_string2, $scaleVal);
    echo "Output with Scale Value is: " . $result;
?>
Output with Scale Value is: 4.5110

Conclusion

The bcdiv() function is essential for precise division operations with arbitrary precision numbers. Using the scale parameter allows you to control the decimal precision of your results, making it ideal for financial calculations where accuracy is crucial.

Updated on: 2026-03-15T09:53:46+05:30

864 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements