Round to a given PHP



  • I need to round up the numbers to the assignment manually, for example:

    131 -> 0
    385 -> 500
    831 -> 1000
    749 -> 500
    

    How can that be done?



  • for a fixed number of points, something like this will happen.

    $points = [0,500,1000];    
    $data   = [131,385,831,749];
    

    $finder = function($v) use ($points) {
    $min = PHP_INT_MAX;
    $res = null;

            foreach($points as $idx => $p){
                if($min > abs($p - $v)){
                    $min = abs($p-$v);
                    $res = $p;
                }
            }
            return $res;
                
          };
    

    foreach($data as $v){
    echo "$v - {$finder($v)}\n";
    }


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2