Skip to content

Instantly share code, notes, and snippets.

@Viper007Bond
Created March 17, 2012 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viper007Bond/2054188 to your computer and use it in GitHub Desktop.
Save Viper007Bond/2054188 to your computer and use it in GitHub Desktop.
Float Parsing Function Tests
<?php
// floatvalue() ended up working best most of the time, with only a few exceptions
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$floatvals = array(
'1,337.01',
'1,337',
'1337.01',
'1337',
'1.337,01',
'1.337',
'1337,01',
'1337',
"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",
'4.339', // This is the tricky one
);
function viper_float_it( $number ) {
$comma_pos = strpos( $number, ',' );
// No commas? Easy!
if ( false === $comma_pos )
return (float) $number;
$period_pos = strpos( $number, '.' );
// No periods but there is a comma
// Comma could be decimal separator but going to assume not. Sorry!
if ( false === $period_pos )
return (float) str_replace( ',', '.', $number );
// If we're still here, there's both comma(s) and period(s)
// Figure out which is being used as the thousands separator
if ( $comma_pos < $period_pos ) {
// This is 1,337.01 format
$number = str_replace( ',', '', $number );
} else {
// This is 1.337,01 format
$number = str_replace( '.', '', $number );
$number = str_replace( ',', '.', $number );
}
return (float) $number;
}
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;
}
function getFloat($pString) {
if (strlen($pString) == 0) {
return false;
}
$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;
}
function floatvalue($value) {
return floatval( preg_replace( '#^([-]*[0-9\.,\' ]+?)((\.|,){1}([0-9-]{1,2}))*$#e', "str_replace(array('.', ',', \"'\", ' '), '', '\\1') . '.\\4'", $value ) );
}
echo "<table border='1'>
<tr>
<th>String</th>
<th>floatval()</th>
<th>getFloat()</th>
<th>parseFloat()</th>
<th>floatvalue()</th>
<th>viper_float_it()</th>
</tr>";
foreach ($floatvals as $fval) {
echo "<tr>";
echo "<td>" . (string) $fval . "</td>";
echo "<td>" . floatval($fval) . "</td>";
echo "<td>" . getFloat($fval) . "</td>";
echo "<td>" . parseFloat($fval) . "</td>";
echo "<td>" . floatvalue($fval) . "</td>";
echo "<td>" . viper_float_it($fval) . "</td>";
echo "</tr>";
}
echo '</table>';
@Viper007Bond
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment