MiCR0 Posted January 7, 2011 Share Posted January 7, 2011 I want to round off an integer vaule to 10-6 grd from 52.71666666 to 52716666 0.926888888 to 926888 does anyone know a simple way to do this? Link to comment https://forums.phpfreaks.com/topic/223671-rounding-an-integer/ Share on other sites More sharing options...
runnerjp Posted January 7, 2011 Share Posted January 7, 2011 Hey! You need to use strlen(); to change length preg_replace() will remove the fulltop Link to comment https://forums.phpfreaks.com/topic/223671-rounding-an-integer/#findComment-1156199 Share on other sites More sharing options...
MiCR0 Posted January 7, 2011 Author Share Posted January 7, 2011 I think you mean substr() however sure I could do that however if there is a better way as this will loop 100,000 of times Link to comment https://forums.phpfreaks.com/topic/223671-rounding-an-integer/#findComment-1156215 Share on other sites More sharing options...
Adam Posted January 7, 2011 Share Posted January 7, 2011 You may want to come up with a more meaningful name for it, but try this function: function removeDecimalAndToSixPlaces($x) { list($number, $decimal) = explode('.', $x); $decimal = substr($decimal, 0, 6); if (empty($number)) { return (int) $decimal; } return (int) $number . $decimal; } echo removeDecimalAndToSixPlaces(52.71666666); //52716666 echo removeDecimalAndToSixPlaces(0.926888888); //926888 There's nothing particular complex within this, so you shouldn't have any performance problems runnning it 100,000 times. Although if this isn't something you plan on re-using, you should take it out of the function wrapper. sbustr couldn't be avoided as numer_format will round up the removed decimal points. Link to comment https://forums.phpfreaks.com/topic/223671-rounding-an-integer/#findComment-1156220 Share on other sites More sharing options...
MiCR0 Posted January 7, 2011 Author Share Posted January 7, 2011 thank you MrAdam Link to comment https://forums.phpfreaks.com/topic/223671-rounding-an-integer/#findComment-1156281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.