[PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8]
floatval — Get float value of a variable
Description
floatval[mixed $value
]: float
Parameters
value
May be any scalar type. floatval[] should not be used on objects, as doing so will emit an E_NOTICE
level error and return 1.
Return Values
The float value of the given variable. Empty arrays return 0, non-empty arrays return 1.
Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of float casting apply.
Examples
Example #1 floatval[] Example
Example #2 floatval[] non-numeric leftmost characters Example
See Also
- boolval[] - Get the boolean value of a variable
- intval[] - Get the integer value of a variable
- strval[] - Get string value of a variable
- settype[] - Set the type of a variable
- Type juggling
brewal dot renault at gmail dot com ¶
8 years ago
This function takes the last comma or dot [if any] to make a clean float, ignoring thousand separator, currency or any other letter :
function tofloat[$num] {
$dotPos = strrpos[$num, '.'];
$commaPos = strrpos[$num, ','];
$sep = [[$dotPos > $commaPos] && $dotPos] ? $dotPos :
[[[$commaPos > $dotPos] && $commaPos] ? $commaPos : false];
if [!$sep] {
return floatval[preg_replace["/[^0-9]/", "", $num]];
}
return floatval[
preg_replace["/[^0-9]/", "", substr[$num, 0, $sep]] . '.' .
preg_replace["/[^0-9]/", "", substr[$num, $sep+1, strlen[$num]]]
];
}
$num = '1.999,369€';
var_dump[tofloat[$num]]; // float[1999.369]
$otherNum = '126,564,789.33 m²';
var_dump[tofloat[$otherNum]]; // float[126564789.33]
Demo : //codepad.org/NW4e9hQH
Anonymous ¶
17 years ago
you can also use typecasting instead of functions:
[float] $value;
Alexey M ¶
6 years ago
There is much easier way to deal with formatted numbers:
double[13232.95]
PapaPinguoin ¶
10 years ago
To view the very large and very small numbers [eg from a database DECIMAL], without displaying scientific notation, or leading zeros.
FR : Pour afficher les très grand et très petits nombres [ex. depuis une base de données DECIMAL], sans afficher la notation scientifique, ni les zéros non significatifs.
anonymous at start dot be ¶
18 years ago
Easier-to-grasp-function for the ',' problem.
chris at georgakopoulos dot com ¶
13 years ago
locale aware floatval:
Michiel ¶
14 years ago
The last getFloat[] function is not completely correct.
1.000.000 and 1,000,000 and its negative variants are not correctly parsed. For the sake of comparing and to make myself clear I use the name parseFloat in stead of getFloat for the new function:
Comparing of float parsing functions with the following function:
steve at opilo dot net ¶
14 years ago
Most of the functions listed here that deal with $ and , are unnecessarily complicated. You can use ereg_replace[] to strip out ALL of the characters that will cause floatval to fail in one simple line of code:
pillepop2003 at yahoo dot de ¶
17 years ago
Use this snippet to extract any float out of a string. You can choose how a single dot is treated with the [bool] 'single_dot_as_decimal' directive.
This function should be able to cover almost all floats that appear in an european environment.
Big Up.
Philipp
secretr at NOSPAM dot e107 dot org ¶
11 years ago
setlocale[] and floatval[] duo could break your DB queries in a very simple way:
You would need simple workaround like:
aa at geb-team dot de ¶
16 years ago
@pillepop2003 at yahoo dot de
use: "/^[0-9-]*[\.]{1}[0-9-]+$/"
instead of: "/^[0-9]*[\.]{1}[0-9-]+$/"
radler63 at hotmail dot com ¶
4 years ago
I get the following disturbing results:
var_dump string[10] "0.01333"
echo the string=0.01333
echo [float]string=0
echo floatval[string]=0
The string is an outcome of array_map['str_getcsv', file[...
I can't find the characters 8-10
thanks
T-Soloveychik at ya.ru ¶
5 years ago
Float value less than 0.0001 [0.0000999999999999995] will be converted by floatval to scientific notation [exponential notation]:
zfcb13 at gmail dot com ¶
6 years ago
More elegant function with selection of decimal point [deafault ,]:
jason at shadonet dot com ¶
19 years ago
Instead of using floatval which only appeared in PHP 4.2 you could juse use $variable = [float]$variable
This function doesn't seem to add any functionality that wasn't already there.
Zipi ¶
19 years ago
This function converts a string to a float no matter is the decimal separator dot [.] or comma [,]. It also converts integers correctly. It takes the digits from the beginning of the string and ignores all other characters.
-Zipi [Finland]
leprau at leprau dot de ¶
15 years ago
For those of you, who are looking for a function that rips the first,
but longest possible float [or at least integer] from a string,
like 123.45 from the string "Price: 123,45$"
If no useable value is found, the function returns false.
Checks for both comma and dot as decimal-separator,
but does not check for 3 digits between thousands,
so 1,234.5 is as valid as 1,23,4.5 [both will return 1234.5]
12,.3 will return 12
1,000,000 will return 1000.0 !
[if thousands separator is defined,
decimals should be defined too ...
in fact I was too lazy to check for that too]
Here you go, and feel free to optimize the function ;]
iliyazelenkog at gmail dot com ¶
3 years ago
[float] would be more performant here [up to 6x times faster].
intval, floatval, doubleval, strva for PHP4 functions [intval, floatval, doubleval, strval], in PHP5 use type casting construction [i.e. '[type] parameter'].
ted devito ¶
13 years ago
i noticed all [well, unless i missed something] the functions working with decimals destroy trailing decimal places. this function restores them in case you want to be able to display a consistent precision for users.
info at marc-gutt dot de ¶
14 years ago