Jump to content

round number, to nearest value ending with either X or Y


WillyTheFish

Recommended Posts

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!!

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

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?

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 :)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.