PHP – How to set or get the default scale parameter for all bc math functions using bcscale() function?

In PHP, the bcscale() function is used to set the default scale parameter for all bc math functions. This function sets the default number of decimal places for all following calls to bc math functions that do not explicitly specify a scale parameter.

Syntax

int bcscale(int $scale)

Parameters

The bcscale() function accepts a single mandatory parameter −

$scale − An integer that specifies the number of digits after the decimal point. The default value is 0.

Return Value

The bcscale() function returns the old scale value as an integer.

Example 1

This example demonstrates how bcscale() sets the default scale for subsequent bc math operations ?

<?php
    // Set default scale to 5 decimal places
    bcscale(5);

    // Uses the default scale value of 5
    echo bcadd('107', '6.5596'), "
"; // Explicitly sets scale to 1, overriding default echo bcadd('107', '6.55957', 1), "
"; // Uses the default scale value of 5 again echo bcadd('107', '6.55957'), "
"; ?>
113.55960
113.5
113.55957

Example 2

This example shows how changing the default scale affects subsequent calculations ?

<?php
    // Set default scale to 5
    bcscale(5);

    // Uses default scale of 5
    echo bcadd('107', '6.5596'), "
"; // Explicitly override with scale of 1 echo bcadd('107', '6.55957', 1), "
"; // Change default scale to 3 bcscale(3); // Now uses the new default scale of 3 echo bcadd('107', '6.55957'), "
"; ?>
113.55960
113.5
113.559

Getting Current Scale

You can retrieve the current default scale by calling bcscale() without parameters ?

<?php
    // Set scale to 3
    bcscale(3);
    
    // Get current scale
    $current_scale = bcscale();
    echo "Current scale: " . $current_scale;
?>
Current scale: 3

Conclusion

The bcscale() function provides a convenient way to set a default precision for all bc math operations. It returns the previous scale value and can be called without parameters to retrieve the current scale setting.

Updated on: 2026-03-15T09:54:38+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements