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