@pillepop2003 at yahoo dot de
<?php
float('-100.00', array('single_dot_as_decimal' => true)); // whoops, returns -10000
?>
use: "/^[0-9-]*[\.]{1}[0-9-]+$/"
instead of: "/^[0-9]*[\.]{1}[0-9-]+$/"
floatval
(PHP 4 >= 4.2.0, PHP 5)
floatval — 変数の float 値を取得する
説明
float floatval ( mixed $var )var の float 値を返します。
パラメータ
- var
あらゆるスカラ型を指定できます。配列あるいはオブジェクトに floatval() を使用することはできません。
返り値
指定した変数の float 値を返します。
例
例 2479. floatval() の例
<?php
$var = '122.34343The';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343
?>
参考
| intval() |
| strval() |
| settype() |
| 型の相互変換 |
floatval
aa at geb-team dot de
05-Sep-2006 12:03
05-Sep-2006 12:03
19-Apr-2005 09:30
you can also use typecasting instead of functions:
(float) $value;
pillepop2003 at yahoo dot de
14-Dec-2004 07:38
14-Dec-2004 07:38
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.
<?php
function float($str, $set=FALSE)
{
if(preg_match("/([0-9\.,-]+)/", $str, $match))
{
// Found number in $str, so set $str that number
$str = $match[0];
if(strstr($str, ','))
{
// A comma exists, that makes it easy, cos we assume it separates the decimal part.
$str = str_replace('.', '', $str); // Erase thousand seps
$str = str_replace(',', '.', $str); // Convert , to . for floatval command
return floatval($str);
}
else
{
// No comma exists, so we have to decide, how a single dot shall be treated
if(preg_match("/^[0-9]*[\.]{1}[0-9-]+$/", $str) == TRUE && $set['single_dot_as_decimal'] == TRUE)
{
// Treat single dot as decimal separator
return floatval($str);
}
else
{
// Else, treat all dots as thousand seps
$str = str_replace('.', '', $str); // Erase thousand seps
return floatval($str);
}
}
}
else
{
// No number found, return zero
return 0;
}
}
// Examples
echo float('foo 123,00 bar'); // returns 123.00
echo float('foo 123.00 bar' array('single_dot_as_decimal'=> TRUE)); //returns 123.000
echo float('foo 123.00 bar' array('single_dot_as_decimal'=> FALSE)); //returns 123000
echo float('foo 222.123.00 bar' array('single_dot_as_decimal'=> TRUE)); //returns 222123000
echo float('foo 222.123.00 bar' array('single_dot_as_decimal'=> FALSE)); //returns 222123000
// The decimal part can also consist of '-'
echo float('foo 123,-- bar'); // returns 123.00
?>
Big Up.
Philipp
anonymous at start dot be
16-Jun-2004 11:00
16-Jun-2004 11:00
Easier-to-grasp-function for the ',' problem.
function Getfloat($str) {
if(strstr($str, ",")) {
$str = str_replace(".", "", $str); // replace dots (thousand seps) with blancs
$str = str_replace(",", ".", $str); // replace ',' with '.'
}
if(preg_match("#([0-9\.]+)#", $str, $match)) { // search for number that may contain '.'
return floatval($match[0]);
} else {
return floatval($str); // take some last chances with floatval
}
}
echo Getfloat("$ 19.332,35-"); // will print: 19332.35
thorcharAThotmailDOTcom
12-Mar-2004 05:40
12-Mar-2004 05:40
Please note that conversion is not always correct, if you use a , instead of a . conversion wil return an integer on most servers.
vickers at hotpop dot com
27-Jan-2004 08:45
27-Jan-2004 08:45
floatval() does not work with "$35,234.43", as it could not handle the '$' and the ','. The following takes care of all values, such that only numeric and the decimal sign are input into floatval(). (It probably shows I'm an old 'c' guy)...this function only lightly tested.
function strtflt($str) {
$il = strlen($str);
$flt = "";
$cstr = "";
for($i=0;$i<$il;$i++) {
$cstr = substr($str, $i, 1);
if(is_numeric($cstr) || $cstr == ".")
$flt = $flt.$cstr;
}
return floatval($flt);
}
Richard Vickers
vickers@hotpop.com
Zipi
25-Apr-2003 06:51
25-Apr-2003 06:51
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.
function floatval($strValue) {
$floatValue = ereg_replace("(^[0-9]*)(\\.|,)([0-9]*)(.*)", "\\1.\\3", $strValue);
if (!is_numeric($floatValue)) $floatValue = ereg_replace("(^[0-9]*)(.*)", "\\1", $strValue);
if (!is_numeric($floatValue)) $floatValue = 0;
return $floatValue;
}
-Zipi (Finland)
jason at shadonet dot com
08-Mar-2003 12:07
08-Mar-2003 12:07
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.