3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $digits = 10; foreach([7887.0000000000000000, 8667887.0000000000000000, 0.0786451, 1.0786451] as $lastPrice) { $price = number_format($lastPrice, 16, '.', ''); $price = ltrim($price, '-'); $price = rtrim($price, '0'); list($d, $s) = explode('.', $price); var_dump($price); var_dump([$d, $s]); if ($d !== '0') { $round = $digits - strlen($d); } else { $round = $digits + strlen($s) - strlen(ltrim($s, '0')); } var_dump($round); } return; foreach ([7887.0000000000000000, 0.0786451] as $price) { $maxDigits = 5; $log = floor(round(log($price) / M_LN10, 10)); var_dump($log); var_dump((10 ** ($maxDigits - $log))); }
based on AZ37R
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.1.0 - 7.3.0alpha1
string(5) "7887." array(2) { [0]=> string(4) "7887" [1]=> string(0) "" } int(6) string(8) "8667887." array(2) { [0]=> string(7) "8667887" [1]=> string(0) "" } int(3) string(9) "0.0786451" array(2) { [0]=> string(1) "0" [1]=> string(7) "0786451" } int(11) string(18) "1.0786450999999999" array(2) { [0]=> string(1) "1" [1]=> string(16) "0786450999999999" } int(9)