Skip to content

Commit

Permalink
Fixes for parse float
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Feb 16, 2024
1 parent 2f8f9ff commit 6445e51
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 4 additions & 7 deletions app/Import/Transformer/BaseTransformer.php
Expand Up @@ -315,14 +315,11 @@ public function hasProduct($key)
public function getFloat($data, $field)
{
if (array_key_exists($field, $data)) {
//$number = preg_replace('/[^0-9-.]+/', '', $data[$field]);
return Number::parseFloat($data[$field]);
} else {
//$number = 0;
return 0;
}

// return Number::parseFloat($number);
}

return 0;

}

/**
Expand Down
12 changes: 11 additions & 1 deletion app/Utils/Number.php
Expand Up @@ -95,6 +95,14 @@ public static function formatValueNoTrailingZeroes($value, $entity): string
*/
public static function parseFloat($value)
{
if(!$value)
return 0;

$multiplier = false;

if(substr($value, 0,1) == '-')
$multiplier = -1;

// convert "," to "."
$s = str_replace(',', '.', $value);

Expand All @@ -108,7 +116,9 @@ public static function parseFloat($value)
// remove all separators from first part and keep the end
$s = str_replace('.', '', substr($s, 0, -3)).substr($s, -3);

// return float
if($multiplier)
$s = floatval($s)*-1;

return (float) $s;
}

Expand Down

0 comments on commit 6445e51

Please sign in to comment.