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

Learn More

Add two arbitrary precision numbers with bcadd() function [bcmath and php-bcmath]

112 Runs 715 Views 42 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * BC Math
 * add two arbitrary precision numbers with bcadd() function
 */

$a = '123.456';
$b = '68';
//adding the two numbers
$resultNoDecimals = bcadd($a, $b);
//adding the two numbers and setting the number of digits after the floating point
$resultWithDecimals = bcadd($a, $b, 3);
echo '<center>';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:500px;font-size:16px">
Adding ' . $a . ' with ' . $b . ' will result: ' . $resultNoDecimals . '</b>
<br />* the result is shown with no decimals after the floating point </div><br />';
echo '<br /><div style="background-color:yellow;color:#000;padding:10px;width:500px;font-size:16px">
Adding ' . $a . ' with ' . $b . ' will result: ' . $resultWithDecimals . '</b>
<br />* the result is shown with decimals after the floating point  
</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).