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

PHPServer Side ProgrammingProgramming

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

Syntax

int bcscale($scale)

Parameters

The $bcscale() parameter accepts only a single parameter and it is the mandatory integer type parameter. This parameter shows the number of digits which come after the decimal. Its default value is 0.

Return Value

The $bcscale() function returns the old scale value.

Example 1

<?php
   // default scale : 5
   bcscale(5);

   // The default scale value as 5
   echo bcadd('107', '6.5596'), "
";    // this is not the same without bcscale()    echo bcadd('107', '6.55957', 1), "
";    // the default scale value as 5    echo bcadd('107', '6.55957'), "
"; ?>

Output

113.55960 113.5 113.55957

Example 2

<?php
   // set default scale 5
   bcscale(5);

   // set the default scale value as 5
   echo bcadd('107', '6.5596'), "
";    // this is not the same without    bcscale()    echo bcadd('107', '6.55957', 1), "
";    // Changed the default scale value    bcscale(3);    // the default scale value as 5    echo bcadd('107', '6.55957'), "
"; ?>

Output

113.55960 113.55 113.559
raja
Updated on 21-Aug-2021 07:37:33

Advertisements