0

Im trying to write a function that will check if a $var is a number (i was trying to trying to use is_int()) and if it is then run round().

The only problem is that for round() im using a number like 123.25, which as in understand isn't an int, because of the the decimal places, so i cant use is_int().

Any idea how i can get this to work ?

$var = 1123.25;

function round_num($var) {
    if(is_int($var)) {
        return round($var, 2);
    }
}

echo round_num($var);
sam
  • 9,486
  • 36
  • 109
  • 160
  • 1
    [`is_numeric()`](http://php.net/manual/en/function.is-numeric.php)? I'd also suggest you add `!empty($var)` to your condition so that it doesn't give a notice if the `$var` is empty – billyonecan May 08 '13 at 12:03

1 Answers1

1

Check using function is_numeric.

function round_num($var) {
    if(is_numeric($var)) {
        return round($var, 2);
    }
}
Rikesh
  • 26,156
  • 14
  • 79
  • 87