3v4l.org

run code in 300+ PHP versions simultaneously
<?php $number = 3.0000000002; $precision = 8; $delta = pow(0.1, $precision); $rounded = round($number, $precision); $roundedResult = $number; echo "Test 1: ".$rounded.PHP_EOL; echo "Test 2: ".abs($number - $rounded).PHP_EOL; if (abs($number - $rounded) < $delta) { $roundedResult = intval($number); } $intedResult = $number; if ((int) $number == $number){ $intedResult = (int) $number; } echo "Delta is : ".$delta.PHP_EOL; echo "Source number: ".$number.PHP_EOL; echo "Rounded result: ".$roundedResult.PHP_EOL; echo "Inted result: ".$intedResult.PHP_EOL;
Output for 5.2.2 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Test 1: 3 Test 2: 2.0000001654807E-10 Delta is : 1.0E-8 Source number: 3.0000000002 Rounded result: 3 Inted result: 3.0000000002
Output for 4.3.10 - 4.3.11, 4.4.0 - 4.4.9, 5.0.2 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.1
Test 1: 3 Test 2: 2.0000001654807E-10 Delta is : 1E-08 Source number: 3.0000000002 Rounded result: 3 Inted result: 3.0000000002
Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.1
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/EBumW on line 16 Test 1: 3PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/EBumW on line 17 Test 2: 2.0000001654807E-10PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/EBumW on line 30 Delta is : 1E-08PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/EBumW on line 32 Source number: 3.0000000002PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/EBumW on line 33 Rounded result: 3PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/EBumW on line 34 Inted result: 3.0000000002PHP_EOL

preferences:
286.71 ms | 403 KiB | 370 Q