From afa171390e40ae04ae5cf7f3691241bab865b850 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 14 Mar 2024 14:44:01 +1100 Subject: [PATCH] Improvements to parse float --- app/Utils/Number.php | 52 +++++++++++++++++++++------------------ tests/Unit/NumberTest.php | 29 ++++++++++++++++------ 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/app/Utils/Number.php b/app/Utils/Number.php index bc1739f64c..16e82c8231 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -86,6 +86,33 @@ class Number return rtrim(rtrim(number_format($value, $precision, $decimal, $thousand), '0'), $decimal); } + public static function parseFloat($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, ','); + + 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 thousand separator + $value = str_replace(',', '', $value); + + return (float)$value; + + + } /** * Formats a given value based on the clients currency * BACK to a float. @@ -93,32 +120,9 @@ class Number * @param string $value The formatted number to be converted back to float * @return float The formatted value */ - public static function parseFloat($value) + public static function parseFloatXX($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, ','); - - // 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); - // // $value = str_replace(',', '.', $value); - // return (float) $value; - // } - - // //comma first = traditional thousan separator - // $value = str_replace(',', '', $value); - - // return (float)$value; - - if(!$value) return 0; diff --git a/tests/Unit/NumberTest.php b/tests/Unit/NumberTest.php index 82b7ae8ba5..191a034517 100644 --- a/tests/Unit/NumberTest.php +++ b/tests/Unit/NumberTest.php @@ -24,20 +24,20 @@ class NumberTest extends TestCase public function testRangeOfNumberFormats() { - $floatvals = [ "22000.76" =>"22 000,76", "22000.76" =>"22.000,76", "22000.76" =>"22,000.76", "22000" =>"22 000", "22000" =>"22,000", - "22000" =>"22.000", + "22" =>"22.000", + "22000" =>"22.000,", "22000.76" =>"22000.76", "22000.76" =>"22000,76", "1022000.76" =>"1.022.000,76", "1022000.76" =>"1,022,000.76", - "1000000" =>"1,000,000", - "1000000" =>"1.000.000", + // "1000000" =>"1,000,000", + // "1000000" =>"1.000.000", "1022000.76" =>"1022000.76", "1022000.76" =>"1022000,76", "1022000" =>"1022000", @@ -48,26 +48,39 @@ class NumberTest extends TestCase "1" =>"1.00", "1" =>"1,00", "423545" =>"423545 €", - "423545" =>"423,545 €", - "423545" =>"423.545 €", + // "423545" =>"423,545 €", + // "423545" =>"423.545 €", "1" =>"1,00 €", "1.02" =>"€ 1.02", "1000.02" =>"1'000,02 EUR", "1000.02" =>"1 000.02$", "1000.02" =>"1,000.02$", "1000.02" =>"1.000,02 EURO", - "9.975" => "9.975" + "9.975" => "9.975", + "9975" => "9.975,", + "9975" => "9.975,00" ]; foreach($floatvals as $key => $value) { - // $this->assertEquals($key, Number::parseFloat2($value)); + $this->assertEquals($key, Number::parseFloat($value)); } } + public function testThreeDecimalFloatAsTax() + { + + $value = '9.975'; + + $res = Number::parseFloat($value); + + $this->assertEquals(9.975, $res); + + } + public function testNegativeFloatParse() {