num1 = " . $num1; // Output: int = 0.65 ...">

Explain php output

Can someone explain the output below in PHP?

$num1 = (1.65 - 1.00);
echo "<br><br>num1 = " . $num1;   // Output: int = 0.65

$num_int1 = $num1 * 100;
echo "<br><br>int = " . $num_int1;   // Output: int = 65

$num_int2 = (int) $num_int1;
echo "<br><br>int = " . $num_int2;   // Output: int = 64

Why is $ num_int2 equal to 64?

Thanks for the help.

+3
source share
1 answer

From the article I wrote for Authorize.Net (in your particular case $num_int1, it is a float, even if it looks like an integer):

One plus one equals two, right? How about .2 plus 1.4 times 10? This is 16, right? Not if you are doing math with PHP (or most other programming languages):

echo floor((0.2 + 1.4) * 10); // Should be 16. But it 15!

, . , , . .2 1,4 10 15,9999999998 . , , . , , , - .

BC

, PHP BC Math, " PHP Binary Calculator, , ". , , . BC Math , , , .

, , bcadd() . . - , , - , . , .

echo floor(bcadd('0.2', '1.4', 2) * 10); // It 16 like we would expect it to be.
+15

All Articles