PHP: How to check if a number has more than two decimals

php

Solution

You could use a regex:

$number = 1.12; //Don't match
$number = 1.123; //Match
$number = 1.1234; //Match
$number = 1.123; //Match

if (preg_match('/\.\d{3,}/', $number)) {
    # Successful match
} else {
    # Match attempt failed
}

Problem

I'm trying to pick out numbers with more than two decimals (more than two digits after the decimal separator). I cant't figure out why this doesn't work: ``` if ($num * 100 != floor($num * 100)) { echo "The number you entered has more than two decimals"; } ``` Why is the number 32.45 picked out, while 32.44 isn't?

Original source