note 31533 added to function.floatval
| From: | Zipi at rack1 dot php dot net | Date: | Fri, 25 Apr 2003 07:51:06 +0000 |
| Subject: | note 31533 added to function.floatval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47063@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.
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://www.php.net/manual/en/function.floatval.php
http://master.php.net/manage/user-notes.php?action=edit+31533
http://master.php.net/manage/user-notes.php?action=delete+31533
http://master.php.net/manage/user-notes.php?action=reject+31533