Speed up development with full-stack environments for every branch.

Learn More

Compare two arbitrary precision numbers with bccomp() function [bcmath and php-bcmath]

133 Runs 760 Views 52 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * BC Math
 * add two arbitrary precision numbers with bcadd() function
 */
//comparing different sets of numbers
$result_1= bccomp('123','234.92');
$result_2= bccomp('250.87','180');
$result_3= bccomp('123','123');
//here the result will be 0 because the scale is set to 4 meaning that 123.0000 
//will be compared with 123 and they are equal
$result_4= bccomp('123.00001','123',4);
echo '<center>';
echo '<br /><div style="background-color:yellow;color:#000;padding:10px;width:500px;font-size:16px">
bccomp(left_operand,right_operand,scale) 
Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand,
-1 otherwise.
</div><br />';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:500px;font-size:16px">
bccomp("123","234.92") will output '.$result_1.'
</div><br />';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:500px;font-size:16px">
bccomp("250.87","180") will output '.$result_2.'
</div><br />';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:500px;font-size:16px">
bccomp("123","123") will output '.$result_3.'
</div><br />';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:500px;font-size:16px">
bccomp("123.00001","123",4) will output '.$result_4.'
</div><br />';
echo '</center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).