denoteone Posted February 28, 2009 Share Posted February 28, 2009 is there a way to round up a number to the half? so if I had 1.26 I would get 1.5 and if I had 1.75 I get 2 that sort of thing. thanks everyone for any feedback. Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/ Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 and only after a floating point E.G. "." Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772967 Share on other sites More sharing options...
denoteone Posted February 28, 2009 Author Share Posted February 28, 2009 the number will always have a "." yes Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772968 Share on other sites More sharing options...
denoteone Posted February 28, 2009 Author Share Posted February 28, 2009 let me correct myself. if it is 1.6 it would go to 1.5 if it was 1.8 it would go to 2 does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772972 Share on other sites More sharing options...
samshel Posted February 28, 2009 Share Posted February 28, 2009 http://us2.php.net/manual/en/function.round.php Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772973 Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 ok deno.. I'm putting 1 together right now.. Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772976 Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 try this.. its not tested <?php function roundP5($num) { if ($d = stristr($num,'.')) { $place = substr($d,1); if (strlen($place) == 1) $place = $place * 10; $zpad = str_repeat('0',strlen($place) - 2); $main = stristr($d,'.',true); if (!$main) $main = 0; if ($place >= ("75".$zpad)) { $place = 0; $main++; } elseif ($place >= ("25".$zpad)) { $place = 5; } elseif ($place < ("25".$zpad)) { $place = 0; } return $main.$place; } return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772982 Share on other sites More sharing options...
laffin Posted February 28, 2009 Share Posted February 28, 2009 I believe ya wud have to strip the whole number and than work with the rounding for the fraction... $num=1.74; $fract=$num-abs($num); than the easiest way is using if..else... construct if($fract>=.75) $fract=1; elsrif($fract>=.25) $fract=.5; else $fract=0; than add the whole number and the new fraction together $num=abs($num)+$fract; well thats the simplest I can think of at the moment; a lot of this can be cut down significantly, but wasnt to keep example of how it can be done and still readable; $num=1.24999; $num=(abs($num))+(($fract=$num-abs($num))>=.75?1:($fract>=.26?.5:0))); careful that was done off the top of my head so there maybe a missing ( or ) but ya gets the idea Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772988 Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 function roundP5($num) { if ($d = stristr($num,'.')) { $place = substr($d,1); if (strlen($place) == 1) $place = $place * 10; $zpad = str_repeat('0',strlen($place) - 2); $main = substr($num,0,strpos($num,'.',0)); if (!$main) $main = 0; if ($place >= ("75".$zpad)) { $place = 0; $main = $main + 1; } elseif ($place >= ("25".$zpad)) { $place = 5; } elseif ($place < ("25".$zpad)) { $place = 0; } return $main.".".$place; } return false; } this is working.. I just tested it Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-772990 Share on other sites More sharing options...
samshel Posted February 28, 2009 Share Posted February 28, 2009 i think i missed something... this function will do the same thing ? http://us2.php.net/manual/en/function.round.php Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773005 Share on other sites More sharing options...
sasa Posted February 28, 2009 Share Posted February 28, 2009 <?php function my_round($num){ return round($num *2 ) / 2; } for ($i = 0.1; $i < 2; $i += 0.1){ echo $i, ' -> ', my_round($i), "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773075 Share on other sites More sharing options...
Psycho Posted February 28, 2009 Share Posted February 28, 2009 EDIT: Sasa beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773082 Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 hes trying to round to .5 intervals.. not like 1.5 to 2.0 1.5 would be 1.5 1.6 would be 1.5 1.4 would be 1.5 1.2 would be 1.0 thats why I wrote the function, everybody knows about round() EDIT:: oo I see what sasa did Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773112 Share on other sites More sharing options...
Psycho Posted February 28, 2009 Share Posted February 28, 2009 hes trying to round to .5 intervals.. .... thats why I wrote the function, everybody knows about round() EDIT:: oo I see what sasa did *** RussellReal extracts foot from mouth *** Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773130 Share on other sites More sharing options...
RussellReal Posted February 28, 2009 Share Posted February 28, 2009 hes trying to round to .5 intervals.. .... thats why I wrote the function, everybody knows about round() EDIT:: oo I see what sasa did *** RussellReal extracts foot from mouth *** *** And then inserts directly up the rectum of mjdamato *** Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773135 Share on other sites More sharing options...
Mark Baker Posted February 28, 2009 Share Posted February 28, 2009 This code implements MS Excel's MROUND() function, which accepts a number to round, and a precision to round it to. function SIGN($number) { if ($number == 0.0) { return 0; } return $number / abs($number); } function MROUND($number,$multiple) { if ((SIGN($number)) == (SIGN($multiple))) { $lowerVal = floor($number / $multiple) * $multiple; $upperVal = ceil($number / $multiple) * $multiple; $adjustUp = abs($number - $upperVal); $adjustDown = abs($number - $lowerVal) + PRECISION; if ($adjustDown < $adjustUp) { return $lowerVal; } return $upperVal; } return False; } echo MROUND(123.45,0.5); Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-773190 Share on other sites More sharing options...
Psycho Posted March 2, 2009 Share Posted March 2, 2009 hes trying to round to .5 intervals.. .... thats why I wrote the function, everybody knows about round() EDIT:: oo I see what sasa did *** RussellReal extracts foot from mouth *** *** And then inserts directly up the rectum of mjdamato *** Grow up, it was just some good natured ribbing. Didn't mean to offend @Mark Baker: You could accomplish the same thing more efficiently using the same logic Sasa provided: function MROUND($num, $unit) { if ($unit==0) { return $num; } $multiplier = 1 / $unit; return round($num * $multiplier ) / $multiplier; } Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-774313 Share on other sites More sharing options...
RussellReal Posted March 2, 2009 Share Posted March 2, 2009 hes trying to round to .5 intervals.. .... thats why I wrote the function, everybody knows about round() EDIT:: oo I see what sasa did *** RussellReal extracts foot from mouth *** *** And then inserts directly up the rectum of mjdamato *** Grow up, it was just some good natured ribbing. Didn't mean to offend yeahhh... lol Quote Link to comment https://forums.phpfreaks.com/topic/147245-math-round/#findComment-774316 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.