FILTER_SANITIZE_NUMBER_FLOAT constant in PHP

The FILTER_SANITIZE_NUMBER_FLOAT constant removes all illegal characters from a float number, keeping only digits, plus/minus signs, and optionally decimal points, thousand separators, or scientific notation based on the flags used.

Syntax

filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, $flags)

Flags

  • FILTER_FLAG_ALLOW_FRACTION − Allows decimal point (.) for fractions

  • FILTER_FLAG_ALLOW_THOUSAND − Allows comma (,) as thousand separator

  • FILTER_FLAG_ALLOW_SCIENTIFIC − Allows e/E for scientific notation

Return Value

Returns the sanitized string with only valid float characters, or FALSE on failure.

Example 1: Using FILTER_FLAG_ALLOW_FRACTION

The following example demonstrates how to sanitize a string while preserving decimal points ?

<?php
    $var = "3-1f+2.56p";
    $result = filter_var($var, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
    var_dump($result);
?>
string(8) "3-1+2.56"

Example 2: Using FILTER_FLAG_ALLOW_THOUSAND

This example shows how to preserve thousand separators during sanitization ?

<?php
    $var = "1-4f+25,6p";
    $result = filter_var($var, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
    var_dump($result);
?>
string(8) "1-4+25,6"

Example 3: Using Multiple Flags

You can combine multiple flags using the bitwise OR operator ?

<?php
    $var = "1,234.56e2abc";
    $result = filter_var($var, FILTER_SANITIZE_NUMBER_FLOAT, 
        FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_SCIENTIFIC);
    var_dump($result);
?>
string(10) "1,234.56e2"

Conclusion

FILTER_SANITIZE_NUMBER_FLOAT is useful for cleaning user input containing numeric data. Use appropriate flags to preserve decimal points, thousand separators, or scientific notation as needed for your application.

Updated on: 2026-03-15T07:34:43+05:30

552 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements