Skip to content

PHP Tutorial Data Types

Floats

Lesson 37 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

A float (floating-point number) is a number with a decimal part.

index.php
PHP
<?php
$price = 19.99;
$pi = 3.14159;
$small = 0.5;

var_dump($price, $pi, $small);
Output
OUTPUT
float(19.99)
float(3.14159)
float(0.5)

Scientific notation

Very large or very small numbers can be written with e.

index.php
PHP
<?php
var_dump(1.5e3);   // 1.5 * 10^3
var_dump(2e-3);    // 2 * 10^-3
Output
OUTPUT
float(1500)
float(0.002)

Floats are approximate

Computers store floats in binary, and many decimal values cannot be stored exactly. This is true in every programming language, not only PHP.

index.php
PHP
<?php
var_dump(0.1 + 0.2);
var_dump(0.1 + 0.2 == 0.3);
Output
OUTPUT
float(0.30000000000000004)
bool(false)

So never compare floats with ==. Compare with a small tolerance instead.

index.php
PHP
<?php
$a = 0.1 + 0.2;
$b = 0.3;

var_dump(abs($a - $b) < 0.00001);
Output
OUTPUT
bool(true)

Floating-Point Pitfalls goes further, including how to handle money.

Rounding for display

round() and number_format() are made for showing floats to people.

index.php
PHP
<?php
$price = 19.987;

echo round($price, 2) . "\n";
echo number_format($price, 2);
Output
OUTPUT
19.99
19.99

Special float values

PHP also has INF (infinity) and NAN (not a number). Check for them with is_infinite() and is_nan().

dangerous
Do not store money as floats
0.1 + 0.2 is not exactly 0.3, and small errors add up. Store prices as integers in the smallest unit (cents or paise), or use a decimal library.