PHP | bccomp() Function

The bccomp() function in PHP is an inbuilt function and is used to compare two arbitrary precision numbers. This function accepts two arbitrary precision numbers as strings and returns the result of comparison of the two numbers after comparing them upto a specified precision.

Syntax:

int bccomp ( $num_str1, $num_str2, $scaleVal)

Parameters: This function accepts three parameters as shown in the above syntax and explained below.



  • $num_str1: This parameter is of string type and represents the left operand or one of the two numbers among which we want to perform the comparison. This parameter is mandatory.
  • $num_str2: This parameter is of string type and represents the right operand or one of the two numbers among which we want to perform the comparison. This parameter is mandatory.
  • $scaleVal: This parameter is of int type and is optional. This parameter tells the number of digits after decimal places that will be used in comparison. The default value of this parameter is zero.

Return Value: This function returns an integral value based on the comparison of the two numbers $num_str1 and $num_str2. If both of the numbers are equal then this function returns zero. If $num_str1 is greater than $num_str2 this function returns 1 and if $num_str1 is less than $num_str2 this function returns -1.

Examples:

Input:  $num_str1 = 3.22, $num_str2 = 3
Output: 0
Explanation: Since the parameter $scaleVal is not 
specified so no digits after decimal is used in 
comparison. So, the value of first parameter which 
is 3.22 will be treated as 3 and hence both 
parameters are equal.

Input:  $num_str1 = 3.222, $num_str2 = 3, $scaleVal = 2
Output: 1

Input:  $num_str1 = 3, $num_str2 = 3.222, $scaleVal = 2
Output: -1

Below programs illustrate the bccomp() function in PHP :

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate bccomp() function
   
// input numbers
$num_str1 = "3.12";  
$num_str2 = "3";  
  
// calculates the comparison of the two 
// numbers when $scaleVal is not specified
$res = bccomp($num_str1, $num_str2, 2);
  
// both parameters are equal
echo $res;
   
?>

chevron_right


Output:

0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate bccomp() function
   
// input numbers
$num_str1 = "3.12";  
$num_str2 = "3";  
  
// scale value
$scaleVal = 2;
  
// calculates the comparison of the two 
// numbers when $scaleVal is specified
$res = bccomp($num_str1, $num_str2, $scaleVal);
  
// first parameter is greater than second
echo $res;
   
?>

chevron_right


Output:

1

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate bccomp() function
   
// input numbers
$num_str1 = "3";  
$num_str2 = "3.12";  
  
// scale value
$scaleVal = 2;
  
// calculates the comparison of the two 
// numbers when $scaleVal is specified
$res = bccomp($num_str1, $num_str2, $scaleVal);
  
// first parameter is smaller than second
echo $res;
   
?>

chevron_right


Output:

-1

Reference:
http://php.net/manual/en/function.bccomp.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.