Check if number is decimal

Check if number is decimal

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Check if Number is Decimal in PHP

Validating user input for decimal numbers, specifically adhering to the US format (X.XXX), requires more than just a simple regex match. While regular expressions offer a quick pattern check, they often struggle with the nuances of floating-point representation and edge cases like trailing zeros or scientific notation. A robust solution must distinguish between a valid decimal string and an integer masquerading as one, while also handling potential parsing errors that can occur when converting strings to floats for business logic.

The Regex Approach vs. Numeric Casting

The most common initial instinct is to use a regular expression. A pattern like /^\d+\.\d+$/ effectively matches a digit, followed by a literal dot, followed by one or more digits. This ensures the string contains exactly one decimal point and no other characters. However, relying solely on this pattern has limitations regarding data integrity.

If your application needs to perform arithmetic immediately after validation, you must convert the validated string to a float. This introduces a well-known pitfall: floating-point precision errors. For instance, 0.1 + 0.2 does not equal 0.3 in binary floating-point arithmetic. If you validate the input as a decimal but then use it in calculations without care, your application might produce incorrect results.

Handling Edge Cases with PHP Functions

A more reliable method for determining if a value is a true decimal often involves checking the type after conversion. You can cast the input string to a float and check its type using gettype() or strict type comparison. However, this approach is fragile because integers like 5 will convert to 5.0, losing the distinction between an integer input and a decimal input unless you validate the string format first.

Here is a practical implementation that combines regex validation with a safety check:

function isValidDecimal($input) {
    // Ensure the input is a string to prevent type coercion issues early on
    $input = (string)$input;
    
    // Regex to match exactly one decimal point surrounded by digits
    // ^ and $ ensure no other characters exist before or after
    if (!preg_match('/^\d+\.\d+$/', $input)) {
        return false;
    }

    // Optional: Validate that it is actually a finite number, not NaN or Inf
    $floatValue = floatval($input);
    return is_finite($floatValue);
}

This function first sanitizes the input to ensure we are treating a string. The regex /^\d+\.\d+$/ enforces the US format strictly: it requires at least one digit before the dot and at least one after. It rejects inputs like .5, 5., or 5..5. Following the regex check, converting to a float and verifying with is_finite() adds an extra layer of safety against malformed data that might pass the regex but represent invalid numerical concepts.

Why Strict Validation Matters

In many legacy systems, developers accept loose validation, assuming that if the database column is defined as DECIMAL, the incoming data is safe. This assumption breaks down when dealing with user inputs from forms or external APIs. Without strict format enforcement, a value of 10 might be stored correctly but treated inconsistently if the system expects a decimal point for all currency values.

Furthermore, consider the case of scientific notation (e.g., 1e5). While mathematically valid, a user interface expecting "US way" decimals likely does not want to see 1e5. The regex pattern above explicitly rejects this by requiring literal digits around the dot, ensuring the output matches user expectations for currency or precise measurements.

Integration with Laravel Ecosystem

If you are working within the Laravel framework, you can leverage built-in helpers or create a custom validation rule. While Laravel's Rule::numeric() accepts integers and decimals, it does not enforce the presence of a decimal point. You would need to write a custom rule that invokes the logic described above.

For high-volume applications where performance is critical, avoid heavy regex patterns on every request if possible. However, for user-facing forms where accuracy is paramount, the slight overhead of a regex check is negligible compared to the cost of data corruption later. Refer to https://laravelcompany.com for best practices on building custom validation rules in Laravel that integrate seamlessly with your existing form requests.

Ultimately, checking if a number is decimal is not just about matching characters; it is about ensuring the data type aligns with business logic expectations while avoiding the hidden traps of floating-point arithmetic.

Our network