1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

improve parseFloat

This commit is contained in:
David Bomba 2024-02-29 12:31:59 +11:00
parent 3ad5de1d2c
commit e22d3effc6

View File

@ -143,6 +143,51 @@ class Number
// return (float) $s;
}
/*
//next iteration of float parsing
public static function parseFloatv2($value)
{
if(!$value) {
return 0;
}
//remove everything except for numbers, decimals, commas and hyphens
$value = preg_replace('/[^0-9.,-]+/', '', $value);
$decimal = strpos($value, '.');
$comma = strpos($value, ',');
//check the 3rd last character
if(!in_array(substr($value, -3, 1), [".", ","])) {
if($comma && (substr($value, -3, 1) != ".")) {
$value .= ".00";
} elseif($decimal && (substr($value, -3, 1) != ",")) {
$value .= ",00";
}
}
$decimal = strpos($value, '.');
$comma = strpos($value, ',');
if($comma === false) { //no comma must be a decimal number already
return (float) $value;
}
if($decimal < $comma) { //decimal before a comma = euro
$value = str_replace(['.',','], ['','.'], $value);
return (float) $value;
}
//comma first = traditional thousan separator
$value = str_replace(',', '', $value);
return (float)$value;
}
*/
public static function parseStringFloat($value)
{
$value = preg_replace('/[^0-9-.]+/', '', $value);