From e22d3effc62cafed2841fa30416586c2c65db497 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 29 Feb 2024 12:31:59 +1100 Subject: [PATCH] improve parseFloat --- app/Utils/Number.php | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/app/Utils/Number.php b/app/Utils/Number.php index ca7a8ad59eca..6a10998d3538 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -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);