note 31533 modified in function.floatval by danbrown
| From: | danbrown@php.net | Date: | Mon, 15 Dec 2008 16:38:09 +0000 |
| Subject: | note 31533 modified in function.floatval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148183@lists.php.net to get a copy of this message | ||
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.
<?php
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)
--was--
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)
http://php.net/manual/en/function.floatval.php