Check if number is decimal

CodeVirtuoso picture CodeVirtuoso · Jul 21, 2011 · Viewed 102.7k times · Source

I need to check in PHP if user entered a decimal number (US way, with decimal point: X.XXX)

Any reliable way to do this?

Answer

cwallenpoole picture cwallenpoole · Jul 21, 2011

You can get most of what you want from is_float, but if you really need to know whether it has a decimal in it, your function above isn't terribly far (albeit the wrong language):

function is_decimal( $val )
{
    return is_numeric( $val ) && floor( $val ) != $val;
}