WillyTheFish Posted February 19, 2010 Share Posted February 19, 2010 hey guys, need some help with this cause i'm too stupid to figure it out myself: function doRound($number,$val1,$val2) { } I want to round a number ($number) so that it ends with either $val1 or $val2, depending on which one is closest to it. for example $number = 14 $val1 = 5 $val2 = 9 result would be 15 $number = 11 $val1 = 5 $val2 = 9 result would be 9 $number = 8 $val1 = 0.5 $val2 = 0.8 result would be 7.8 you get the point please help!! Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/ Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 did u look at round() ceil() floor() number_format() Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014839 Share on other sites More sharing options...
WillyTheFish Posted February 19, 2010 Author Share Posted February 19, 2010 round() and ceil() yes, but couldn't figure out how to use them on this... sorry, I'm still a noob Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014848 Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 Direct from manual: ceil() echo ceil(4.3); // 5 echo ceil(9.999); // 10 floor() echo floor(4.3); // 4 echo floor(9.999); // 9 round() echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 u can download manual here http://lt2.php.net/get/php_manual_en.chm/from/a/mirror Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014857 Share on other sites More sharing options...
WillyTheFish Posted February 19, 2010 Author Share Posted February 19, 2010 thanks, but i knew how ceil and floor works.. the thing is, i need to figure out a logic that works in the example i posted above... Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014867 Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 could u explane a bit more? How/Why $number = 8 $val1 = 0.5 $val2 = 0.8 truns to 7.8 ? or how 14, 9 5 turns to 15? I dont get it. Also I am interested fallowing by your examples should these dRound(14,9,5) dRound(14,5,9) be equal? Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014870 Share on other sites More sharing options...
WillyTheFish Posted February 19, 2010 Author Share Posted February 19, 2010 hi, sure =) i want the value $number to END with either $val1 or $val2 so if if $number is 14, and $val1 = 5 and $val2 = 9 i want $number to be rounden to 15, instead of 19, because it's closer to the original value... don't know how to explain it better then that Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014873 Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 ok first example u explaned well; but how about $number = 11 $val1 = 5 $val2 = 9 15 19 right? then how it turns to 9 ? 15 is closer to 11 then 19 isn't ? third example absolute random where the 7? comes from? I wrote this function but I am not sure that it's what u need function dorand($number, $v1, $v2) { $last = (($number/10)-floor($number/10))*10; $a = (floor($number/10) + $v1/10) * 10; $b = (floor($number/10) + $v2/10) * 10; if(abs($number - $a) < abs($number - $b)) { return $a; } else { return $b; } } Maybe u could show what u realy want to do? and how the real data looks. Link to comment https://forums.phpfreaks.com/topic/192636-round-number-to-nearest-value-ending-with-either-x-or-y/#findComment-1014899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.