简体   繁体   English

检查十进制$ var is_int是否为空,然后运行round()

[英]check if decimal $var is_int, then run round()

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(). 我试图编写一个函数,该函数将检查$ var是否为数字(我试图使用is_int()),然后再运行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(). 唯一的问题是,对于round(),我使用的数字类似于123.25,由于小数点后的位数,因此不是整数,因此我不能使用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 . 使用函数is_numeric进行检查。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM