Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

floatval

Converts a value to a float.

Syntax

floatval(mixed $value): float

Parameters

ParameterTypeDescription
$valuemixedA string or a number.

Return value

floatA float; `0` if the conversion failed.

Examples

A string holding a float:

<?php
 
echo floatval('3.14') * 2, "\n";
echo floatval('12.5 kg');

Output:

6.28
12.5

A comma instead of a dot is not recognised:

<?php
 
echo floatval('3,14'), "\n";
echo floatval(str_replace(',', '.', '3,14'));

Output:

3
3.14

Notes

Only a dot separates the fraction. Numbers typed with a comma have to be fixed before the conversion.

See also

  • intval — Converts a value to an integer.
  • round — Rounds a number to a given number of digits.
  • is_numeric — Checks whether a value is a number or a numeric string.

In the tutorial

  • Data types — The main PHP data types — strings, numbers, booleans, arrays and null.

Thanks for reading!

Donate to the site — we work hard for you.