문제

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);
도움이 되었습니까?

해결책

Check using function is_numeric.

function round_num($var) {
    if(is_numeric($var)) {
        return round($var, 2);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top