0

In the following code, why does $final returns value as float?

When the calculated value is 6, it should return as integer, otherwise float.

How can I do that?

$x = 62;
$round = 5 * round($x / 5);
$final = $round/10;     
var_dump($final); 
float(6) 

Edit: Sorry if my question is not clear. I need to find out if the $final has any decimal value or not. So in order to find out that, I was using is_float function, but that always returns true because above variable returns the value in float always. Hope my question is a bit more clear.

3
  • php types the variables along with their usage. There's no strong beforehand typing. You could use intval afterwards though. (if intval($final) == $final)... Commented Sep 30, 2013 at 11:30
  • 1
    So you have to check whether you got exactly 6 (and intval($final) that), otherwise got float ... where is the problem? Commented Sep 30, 2013 at 11:31
  • If you are working with float numbers you should consider using the BC Math functions - php.net/manual/en/book.bc.php Commented Sep 30, 2013 at 11:51

4 Answers 4

2

The return type of round is float, as the manual states. You need to cast the result to integer:

$result = (integer) round($value);

Update:

Although I think a method should return either floats or integers and not change this depending on the result's value, you could try something like this:

if((integer) $result == $result) {
    $result = (integer) $result;
}
return $result;
Sign up to request clarification or add additional context in comments.

8 Comments

This does not return float (as wanted by OP), if value is e.g. 6.21, it should(!) return as float.
how should the result of round being 6.21 ?
I understood the question like if the result is 6.00 return INT 6, if the result is 6.21, return float 6.21.
If you ask me, the question is questionable ;)
This is clear to me, at least me ;) ... "When the calculated value is 6, it should return as integer, otherwise float."
|
1

See this code live here:

http://sandbox.onlinephpfunctions.com/code/f8f383604cf848ab63534da69295f8482528e4ce

-

<?php

    $final = function($num) {

      $calc = (5 * ($num / 5)) / 10;

      if (intval($calc) == $calc) { settype($calc, "integer"); }
      else { settype($calc, "float"); } // might not need that line
      return $calc;
    };

    var_dump($final(62)); print '<br />';
    var_dump($final(60)); print '<br />';
    var_dump($final(59)); print '<br />';

?>

Besides, in the OPs code, this line (the round())is incorrect:

$round = 5 * round($x / 5);

1 Comment

Thanks to myself for spending (did I say waisting?) my time on that ;)
0

Little bit long, but something like this should do the trick

function ReturnType($FinalVal){
 switch (gettype($FinalVal)){
  case 'integer':
     return (int) $FinalVal;
  case 'double':
    return (float) $FinalVal;

 }
}

 $x = 62;
$round = 5 * round($x / 5);
$final = $round/10;     
$Value = ReturnType($final);

1 Comment

No, guess not correct, see sandbox.onlinephpfunctions.com/code/…
-1

You must casting like this

$round = 5 * (int)(round($x / 5));

round($x / 5) returns float. If you don't cast it to int then the result would be float

1 Comment

and this was NOT the question ... it should return int or float, not only int ... 5* INT is ... still int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.