In PHP, the bccomp() function is used to compare two arbitrary numbers. bccomp()The function accepts two arbitrary-precision numeric strings as input and outputs an integer after comparing the two numbers.
int bccomp($left_string1, $right_string1, $scaleval)
Functionbccomp()Accepts three different parameters− $left_string1, $right_string2 and $scaleval.
$left_string1− represents the left operand of one of the two given numbers we want to compare, which is a parameter of type string.
$right_string2− represents the right operand of one of the two given numbers we want to compare, which is a parameter of type string.
$scaleval−Returns the number of decimal places that will be used in the comparison. It is an argument of type integer. The default value is zero.
Functionbccomp() Returns two numbers $left_string1 and $right_string2 comparison results.
If $left_string1 is greater than $right_string2, return 1.
If $left_string1 is less than $right_string2, return -1.
If the two given numbers are equal, the function bccomp() returns 0.
<?php // input two numbers $left_string1 = "3.12"; $right_string2 = "3"; // calculates the comparison of the two //number without scale value $result = bccomp($left_string1, $right_string2); //used equal parameters echo "The result is: ", $result; ?>
The result is: 0
The above program returns 0 because there is no proportional value in case where equal parameters are used.
<?php // input two numbers $left_string1 = "30.12"; // left value > right value $right_string2 = "3"; //used scale value two $scaleval = 2; // calculates the comparison of the two //number without scale value $result = bccomp($left_string1, $right_string2); //used equal parameters echo "The output is: ", $result; ?>
The output is: 1
It returns 1 because the lvalue is greater than the rvalue.
<?php // input two numbers $left_string1 = "30.12"; $right_string2 = "35"; // Right value > Left value //used scale value two $scaleval = 2; // calculates the comparison of the two //number without scale value $result = bccomp($left_string1, $right_string2); //used equal parameters echo $result; ?>
-1
It returns -1 because the Right value is greater than the Left value.
The above is the detailed content of PHP - How to compare two arbitrary precision numbers using bccomp() function?. For more information, please follow other related articles on the PHP Chinese website!