Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Types and Type Handling

The PHP float type

Reference for the PHP float type: literals, IEEE 754 precision, comparing with a tolerance, NAN and INF, rounding, formatting and float-to-int conversion.

A float (also called double) is a 64-bit IEEE 754 floating-point number. It covers roughly 15 to 17 significant decimal digits and a huge range, about 1.8e308 at the top. Many decimal fractions, such as 0.1, cannot be stored exactly in binary, so small errors appear in calculations.

Treat floats as approximations. Round for display, compare with a tolerance, and keep money in integer cents or in a decimal library or bcmath.

PHP
$price = 19.99;
$big = 1.5e6;        // 1500000.0
$small = 2.5E-3;     // 0.0025

Try it

PHP Precision and comparisons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP NAN, INF and conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$a = 1.234;    $b = 1.2e3;    $c = 7E-10;    $d = 1_000.5;
$e = (float) $value;   // or (double), floatval($value)

Constants: PHP_FLOAT_EPSILON (smallest difference from 1.0), PHP_FLOAT_MAX, PHP_FLOAT_MIN, PHP_FLOAT_DIG, plus INF and NAN.

Usage notes

  • Never compare computed floats with ==. Use abs($a - $b) < $epsilon with a tolerance that fits your data. PHP_FLOAT_EPSILON is the right scale for numbers near 1 only.
  • NAN is not equal to anything, not even itself. Test with is_nan(). INF comes from overflow and fdiv(1, 0); 1 / 0 throws DivisionByZeroError.
  • Display uses precision / serialize_precision. echo 0.1 + 0.2 prints 0.3 because echo rounds to 14 digits (the precision setting), while var_dump and json_encode print the shortest string that round-trips (0.30000000000000004).
  • round() rounds half away from zero (round(2.5) is 3, round(-2.5) is -3). Because the stored value is only an approximation of the decimal you typed, rounding ties at many digits can still disagree with your expectation. For money use integers.
  • Float to int conversion truncates and is meaningless for NAN, INF or values beyond the int range. Deprecations apply when a float with a fractional part is used where an int is required (array keys, %, bit operations).
  • Integers beyond 2^53 lose exactness as floats. Keep IDs as ints or strings.
  • Formatting: number_format(), sprintf("%.2f") and NumberFormatter (intl) are for output; they return strings.
  • Locale-independent. Since PHP 8, float-to-string conversion no longer depends on the locale, so you always get a . as decimal separator.
  • % converts operands to int. 7.5 % 2 is 1 (with a deprecation notice, since the fraction is lost), and 1 % 0.5 throws DivisionByZeroError because the divisor becomes 0. Use fmod() for floats.