devWhiz Posted May 23, 2011 Share Posted May 23, 2011 <?php $a = 350000; $b = 3600000000; echo $hmm = $a / $b; outputs 9.7222222221617E-5 while <?php $a = 350000; $b = 360000000; echo $hmm = $a / $b; outputs 0.000972222222 its only one digit shorter, how can I get the first code to produce the same results, Ive tried using float, int maxes out and Ive changed my floating point in php.ini, no luck Quote Link to comment https://forums.phpfreaks.com/topic/237179-integer-to-big-php-math-calculations/ Share on other sites More sharing options...
anupamsaha Posted May 23, 2011 Share Posted May 23, 2011 Try this: <?php $a = 350000; $b = 360000000; $hmm = $a / $b; echo sprintf("%f", $hmm); Quote Link to comment https://forums.phpfreaks.com/topic/237179-integer-to-big-php-math-calculations/#findComment-1218936 Share on other sites More sharing options...
devWhiz Posted May 23, 2011 Author Share Posted May 23, 2011 ok that works but it shortens up the decimal and rounds it, is there a way to have it calculate the decimal without rounding it? like 0.00092144444443 rounds to 0.000920 how can I get it to keep it at 0.00092144444443 Quote Link to comment https://forums.phpfreaks.com/topic/237179-integer-to-big-php-math-calculations/#findComment-1219100 Share on other sites More sharing options...
anupamsaha Posted May 23, 2011 Share Posted May 23, 2011 <?php $a = 350000; $b = 360000000; $hmm = $a / $b; echo sprintf("%.10f", $hmm); // change 10 to any number that you want to display in the result after the decimal ?> Quote Link to comment https://forums.phpfreaks.com/topic/237179-integer-to-big-php-math-calculations/#findComment-1219129 Share on other sites More sharing options...
ignace Posted May 23, 2011 Share Posted May 23, 2011 bcsub($a,$b); ok that works but it shortens up the decimal and rounds it, is there a way to have it calculate the decimal without rounding it? like 0.00092144444443 rounds to 0.000920 how can I get it to keep it at 0.00092144444443 The correct result of 350000 / 360000000 = 0.0009722222222.. not 0.0009214444.. The reason to why you get an incorrect output is due to the precision loss of a float. Using the BCMath module of PHP you get: echo bcdiv(350000, 360000000, 10); // 0.0009722222 10 is the level of precision you need. Quote Link to comment https://forums.phpfreaks.com/topic/237179-integer-to-big-php-math-calculations/#findComment-1219166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.