danscreations Posted April 8, 2009 Share Posted April 8, 2009 Figuring you could use something in conjunction with the round function but have pulled nothing off searches. Basicly want to take an inputed number and round it to the nearest 1/8" (0.125"). Quote Link to comment https://forums.phpfreaks.com/topic/153212-round-to-the-nearest-18/ Share on other sites More sharing options...
laffin Posted April 8, 2009 Share Posted April 8, 2009 mayby something like val=9.3; nval=int((val*+.5) /8; echo nval; I think that shud work. why the * 8 and / 8? (.5 is used for rounding) A trick i picked up here in the forums to round every half, u can get the rounded version of the number doubled (*2) so to get 8ths, u need to double our double and doublt it again *2 = halfs *4 = quarters *8 = Eights *16 = Sixteenths Quote Link to comment https://forums.phpfreaks.com/topic/153212-round-to-the-nearest-18/#findComment-804853 Share on other sites More sharing options...
kenrbnsn Posted April 8, 2009 Share Posted April 8, 2009 If you looked at the manual page for round, the first example is probably what you're looking for: <?php function round_to($number, $increments) { $increments = 1 / $increments; return (round($number * $increments) / $increments); } ?> For example: <?php $n = 5.3; echo round_to($n, 0.5); // 5.5 ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/153212-round-to-the-nearest-18/#findComment-804854 Share on other sites More sharing options...
micah1701 Posted April 8, 2009 Share Posted April 8, 2009 or you could get all complicated and do something like: <?php $n = 3.3912; $rounded = round($n,3); //shorten to 3 decimal places $parts = explode(".",$rounded); $decimal = $parts[1]; // = 391 $divided = round($decimal/ 125); // = 3 $new_decimal = $divided * .125; // = 0.375 $n = $parts[0] + $new_decimal; // = 3.375 echo $n; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153212-round-to-the-nearest-18/#findComment-804870 Share on other sites More sharing options...
danscreations Posted April 8, 2009 Author Share Posted April 8, 2009 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/153212-round-to-the-nearest-18/#findComment-804909 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.