taith Posted April 19, 2007 Share Posted April 19, 2007 anyone know an easy way to round massive numbers? for example... echo round(78906898968967.432324); outputs "7.8906898969E+013" which is so totally unhelpful... Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/ Share on other sites More sharing options...
per1os Posted April 19, 2007 Share Posted April 19, 2007 http://us.php.net/round <?php echo round(78906898968967.432324, 2); ?> See also: http://us.php.net/manual/en/function.ceil.php http://us.php.net/manual/en/function.floor.php Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-233654 Share on other sites More sharing options...
Psycho Posted April 19, 2007 Share Posted April 19, 2007 echo number_format(78906898968967.432324, 0); Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-233655 Share on other sites More sharing options...
taith Posted April 19, 2007 Author Share Posted April 19, 2007 yes... but the scientific notation is not turnoffable... echo round(78906898968967.432324, 2); outputs "7.8906898969E+013" i need "78906898968967.43" Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-233656 Share on other sites More sharing options...
taith Posted April 19, 2007 Author Share Posted April 19, 2007 i've made this... it works... sorta... function realround($string,$decimal=2){ if(!is_numeric($string)) return $string; $string=explode(".",$string); if(count($string)==1) return $string[0]; $string[1]='1.'.$string[1]; $string[1]=round($string[1],$decimal); $string[1]=substr($string[1],2); return implode(".",$string); } any better suggestions? Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-233659 Share on other sites More sharing options...
Psycho Posted April 20, 2007 Share Posted April 20, 2007 Um, did you try my suggestion of using number_format()? It worked for me. Since you need two decimal places use this: echo number_format(78906898968967.432324, 2); Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-233711 Share on other sites More sharing options...
taith Posted April 20, 2007 Author Share Posted April 20, 2007 yes... but that adds ,'s in... i need realnumbers... true, i can remove them easy enough... question comes in... why does round() give scientific notation by default? shouldnt that be a seperate option? Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-233856 Share on other sites More sharing options...
Psycho Posted April 20, 2007 Share Posted April 20, 2007 C'mon partner, don't expect us to do ALL the work for you. If you had taken just 30 seconds to look at the manual for number_format() you would have seen that you can customize the thousands seperator (which also means you can remove it). http://us2.php.net/manual/en/function.number-format.php <?php echo number_format(78906898968967.432324, 2, '.', '') // Output would be 78906898968967.44 ?> Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-234119 Share on other sites More sharing options...
taith Posted April 20, 2007 Author Share Posted April 20, 2007 bah... too simple... i still say we should contact php mainframe programmers and have the scientific notation added as an option only... in fact... i think i will :-) Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-234122 Share on other sites More sharing options...
Psycho Posted April 20, 2007 Share Posted April 20, 2007 Uh...no. Round is rounding a number and is returning the value of that number. It is returning the value in the most efficient method. It is not intended for format a number. Unless you are presenting a number on the screen it doesn't matter what format it uses internally. That is where number_format comes in. Link to comment https://forums.phpfreaks.com/topic/47818-rounding-massive-numbers/#findComment-234124 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.