note 84793 added to function.floatval
| From: | Michiel at osu1 dot php dot net | Date: | Wed, 30 Jul 2008 12:21:45 +0000 |
| Subject: | note 84793 added to function.floatval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142925@lists.php.net to get a copy of this message | ||
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:
function parseFloat($ptString) {
if (strlen($ptString) == 0) {
return false;
}
$pString = str_replace(" ", "", $ptString);
if (substr_count($pString, ",") > 1)
$pString = str_replace(",", "", $pString);
if (substr_count($pString, ".") > 1)
$pString = str_replace(".", "", $pString);
$pregResult = array();
$commaset = strpos($pString,',');
if ($commaset === false) {$commaset = -1;}
$pointset = strpos($pString,'.');
if ($pointset === false) {$pointset = -1;}
$pregResultA = array();
$pregResultB = array();
if ($pointset < $commaset) {
preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA);
}
preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB);
if ((isset($pregResultA[0]) && (!isset($pregResultB[0])
|| strstr($preResultA[0],$pregResultB[0]) == 0
|| !$pointset))) {
$numberString = $pregResultA[0];
$numberString = str_replace('.','',$numberString);
$numberString = str_replace(',','.',$numberString);
}
elseif (isset($pregResultB[0]) && (!isset($pregResultA[0])
|| strstr($pregResultB[0],$preResultA[0]) == 0
|| !$commaset)) {
$numberString = $pregResultB[0];
$numberString = str_replace(',','',$numberString);
}
else {
return false;
}
$result = (float)$numberString;
return $result;
}
Comparing of float parsing functions with the following function:
function testFloatParsing() {
$floatvals = array(
"22 000,76",
"22.000,76",
"22,000.76",
"22 000",
"22,000",
"22.000",
"22000.76",
"22000,76",
"1.022.000,76",
"1,022,000.76",
"1,000,000",
"1.000.000",
"1022000.76",
"1022000,76",
"1022000",
"0.76",
"0,76",
"0.00",
"0,00",
"1.00",
"1,00",
"-22 000,76",
"-22.000,76",
"-22,000.76",
"-22 000",
"-22,000",
"-22.000",
"-22000.76",
"-22000,76",
"-1.022.000,76",
"-1,022,000.76",
"-1,000,000",
"-1.000.000",
"-1022000.76",
"-1022000,76",
"-1022000",
"-0.76",
"-0,76",
"-0.00",
"-0,00",
"-1.00",
"-1,00"
);
echo "<table>
<tr>
<th>String</th>
<th>floatval()</th>
<th>getFloat()</th>
<th>parseFloat()</th>
</tr>";
foreach ($floatvals as $fval) {
echo "<tr>";
echo "<td>" . (string) $fval . "</td>";
echo "<td>" . (float) floatval($fval) . "</td>";
echo "<td>" . (float) getFloat($fval) . "</td>";
echo "<td>" . (float) parseFloat($fval) . "</td>";
echo "</tr>";
}
echo "</table>";
}
----
Server IP: 89.248.170.61
Probable Submitter: 81.207.252.196
----
Manual Page -- http://www.php.net/manual/en/function.floatval.php
Edit -- https://master.php.net/note/edit/84793
Del: integrated -- https://master.php.net/note/delete/84793/integrated
Del: useless -- https://master.php.net/note/delete/84793/useless
Del: bad code -- https://master.php.net/note/delete/84793/bad+code
Del: spam -- https://master.php.net/note/delete/84793/spam
Del: non-english -- https://master.php.net/note/delete/84793/non-english
Del: in docs -- https://master.php.net/note/delete/84793/in+docs
Del: other reasons-- https://master.php.net/note/delete/84793
Reject -- https://master.php.net/note/reject/84793
Search -- https://master.php.net/manage/user-notes.php