PHP floatval() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, floatval() converts a value to a floating-point number (a number with decimals) and returns it. It does not change the original variable.

For strings, floatval() reads the number from the start of the text and ignores anything after it. If the text does not start with a number, the result is 0. It is the preferred name for the alias doubleval().

Syntax

Call floatval() with the values you need. Start with this line.

PHP
$number = floatval($value);
CallWhat it does
floatval("12.5")Returns 12.5, the string converted to a float.
floatval("1.5abc")Returns 1.5. PHP stops reading at the first letter.
floatval("abc")Returns 0.0, because there is no number at the start.

floatval() is the same as the (float) cast. It never throws an error, even for text that is not a number. Use is_numeric() first if you need to know whether the text was a real number.

Library declarations
PHP
floatval(mixed $value): float

Parameters

ParameterDescription
valueRequired. The value to convert. Scalars are converted normally. Arrays give 0.0 when empty and 1.0 otherwise. Objects are not supported.

Return Value

Return ValueDescription
floatThe float value of the input. Text without a leading number returns 0.0.

PHP Examples

Example 1 — Converting Text to Floats

Use floatval() to turn strings and integers into floats, and check the result with var_dump().

PHP
<?php
var_dump(floatval("12.5"));
var_dump(floatval("1.5abc"));
var_dump(floatval("abc"));
var_dump(floatval(10));

How It Works

  1. "12. 5" is a clean number, so the result is float(12.5).
  2. "1. 5abc" starts with 1.5, so PHP keeps 1.5 and ignores the letters.
  3. "abc" has no leading number, so the result is float(0).
  4. The integer 10 is turned into the float 10. var_dump() shows floats without a trailing .0.

Example 2 — Scientific Notation and Symbols

Some strings are read as numbers and some are not. Try a few unusual ones.

PHP
<?php
var_dump(floatval("1e3"));
var_dump(floatval("-.5"));
var_dump(floatval('$12.50'));
var_dump(floatval(null));

How It Works

  1. "1e3" means 1 times 10 to the power of 3, so the result is 1000.
  2. "-. 5" is a valid negative number even without a leading zero, so it becomes -0.5.
  3. A dollar sign at the start stops the conversion, so '$12. 50' returns 0. Remove the symbol first.
  4. null converts to 0. 0.

Example 3 — Totals and Float Comparison

Convert a price string, multiply it, and see why floats should not be compared with ==.

PHP
<?php
$unit = floatval("2.50");
$total = $unit * 4;
echo "Total: ", number_format($total, 2), PHP_EOL;

$sum = floatval("0.1") + floatval("0.2");
var_dump($sum == 0.3);
var_dump(abs($sum - 0.3) < 0.00001);

How It Works

  1. floatval("2. 50") gives 2.5, which is multiplied by 4 to get 10.
  2. number_format($total, 2) prints the total with two decimals: 10. 00.
  3. 0. 1 + 0.2 is stored with a tiny rounding error, so comparing it to 0.3 with == gives false.
  4. To compare floats safely, check that the difference is smaller than a very small number.

Frequently Asked Questions

floatval() converts a value to a float. For strings it reads the number at the start of the text. Anything after the number is ignored, and text with no leading number becomes 0.
They produce the same result. (float) is a cast written inline, while floatval() is a function, so you can pass it by name to array_map(), for example array_map('floatval', $values).
floatval() stops at the comma and returns 1. Remove the comma first with str_replace(',', '', $text) and then call floatval(). For many locales you may need number parsing tools from the intl extension.
floatval() keeps the decimal part (12.9 stays 12.9), while intval() cuts it off (12.9 becomes 12). Choose the one that matches the type of number you need.
None. doubleval() is an alias of floatval(). floatval() is the preferred name because PHP uses the word float for this type everywhere else.

Next: get_debug_type()

get_debug_type() returns the type name of a value in a form that is easy to read and good .

get_debug_type() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful