Fixes for parse float

This commit is contained in:
David Bomba 2024-02-16 14:28:57 +11:00
parent 2f8f9ffce8
commit 6445e518dc
2 changed files with 15 additions and 8 deletions

View File

@ -315,14 +315,11 @@ class BaseTransformer
public function getFloat($data, $field) public function getFloat($data, $field)
{ {
if (array_key_exists($field, $data)) { if (array_key_exists($field, $data)) {
//$number = preg_replace('/[^0-9-.]+/', '', $data[$field]);
return Number::parseFloat($data[$field]); return Number::parseFloat($data[$field]);
} else {
//$number = 0;
return 0;
} }
// return Number::parseFloat($number); return 0;
} }
/** /**

View File

@ -95,6 +95,14 @@ class Number
*/ */
public static function parseFloat($value) public static function parseFloat($value)
{ {
if(!$value)
return 0;
$multiplier = false;
if(substr($value, 0,1) == '-')
$multiplier = -1;
// convert "," to "." // convert "," to "."
$s = str_replace(',', '.', $value); $s = str_replace(',', '.', $value);
@ -108,7 +116,9 @@ class Number
// remove all separators from first part and keep the end // remove all separators from first part and keep the end
$s = str_replace('.', '', substr($s, 0, -3)).substr($s, -3); $s = str_replace('.', '', substr($s, 0, -3)).substr($s, -3);
// return float if($multiplier)
$s = floatval($s)*-1;
return (float) $s; return (float) $s;
} }