tags:

views:

18

answers:

3

I need to parse a number that has 3 s.f. and 2 d.p. (e.g. 2.34). The first two numbers (1st and 2nd s.f.) are then multiplied by 10. The second number stays as it is. Thus, for the example above, $a = 23 and $b = 4. I was able to get code similar to below to work when it was not a function but I would like to be able to incorporate the code into a function. I would like to be able to get the value $a and the value $b out of the function (separately if possible) but I am having problems doing this. Any help would be much appreciated.

$z = 1.63;
function getcoordinates($conv) {
 $z = number_format($conv, 2);
$z = (string)$z;
$a = substr($z,0,3)*10;
$b = substr($z,3,1);
$a = settype($a, "float");
$b = settype($b, "float");
return $a;
return $b;
}
A: 

Return an array.

return array($a, $b);

and, on the call site, receive the value with

list($a, $b) = getcoordinates(...);
KennyTM
+2  A: 

Why can’t you just calculate the values the way you described?

function getCoordinates($input) {
    $a = floor($input * 10) % 100;
    $b = floor($input * 100) % 10;
    return array($a, $b);
}

As demonstrated here, you can use array() to return multiple values. When you call the function, you can use list() to put the values into separate variables:

list($a, $b) = getCoordinates(2.34);
echo "$a, $b\n";    // prints “23, 4”
Timwi
Thank you - much more elegant function also.
Adam Lund
A: 
function getcoordinates($conv) {
  /* your code */
  return array($a,$b);
}
isola009