diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 26f821750b9..0cf452d683c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -5159,7 +5159,7 @@ function price2num($amount, $rounding = '', $option = 0) // Convert value to universal number format (no thousand separator, '.' as decimal separator) if ($option != 1) { // If not a PHP number or unknown, we change or clean format - //print 'PP'.$amount.' - '.$dec.' - '.$thousand.' - '.intval($amount).'
'; + //print "\n".'PP'.$amount.' - '.$dec.' - '.$thousand.' - '.intval($amount).'
'; if (!is_numeric($amount)) { $amount = preg_replace('/[a-zA-Z\/\\\*\(\)\<\>\_]/', '', $amount); } @@ -5188,6 +5188,7 @@ function price2num($amount, $rounding = '', $option = 0) $amount = str_replace($thousand, '', $amount); // Replace of thousand before replace of dec to avoid pb if thousand is . $amount = str_replace($dec, '.', $amount); } + //print ' XX'.$amount.' '.$rounding; // Now, make a rounding if required if ($rounding) { @@ -5205,13 +5206,13 @@ function price2num($amount, $rounding = '', $option = 0) } elseif (is_numeric($rounding)) { $nbofdectoround = (int) $rounding; } - //print "RR".$amount.' - '.$nbofdectoround.'
'; + //print " RR".$amount.' - '.$nbofdectoround.'
'; if (dol_strlen($nbofdectoround)) { $amount = round(is_string($amount) ? (float) $amount : $amount, $nbofdectoround); // $nbofdectoround can be 0. } else { return 'ErrorBadParameterProvidedToFunction'; } - //print 'SS'.$amount.' - '.$nbofdec.' - '.$dec.' - '.$thousand.' - '.$nbofdectoround.'
'; + //print ' SS'.$amount.' - '.$nbofdec.' - '.$dec.' - '.$thousand.' - '.$nbofdectoround.'
'; // Convert amount to format with dolibarr dec and thousand (this is because PHP convert a number // to format defined by LC_NUMERIC after a calculation and we want source format to be defined by Dolibarr setup. diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index f1341c9a042..e43453b26ec 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -108,7 +108,15 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite. if (! function_exists('mb_substr')) { - print "\n".__METHOD__." function mb_substr must be enabled.\n"; die(); + print "\n".__METHOD__." function mb_substr must be enabled.\n"; die(1); + } + + if ($conf->global->MAIN_MAX_DECIMALS_UNIT != 5) { + print "\n".__METHOD__." bad setup for number of digits for unit amount. Must be 5 for this test.\n"; die(1); + } + + if ($conf->global->MAIN_MAX_DECIMALS_TOT != 2) { + print "\n".__METHOD__." bad setup for number of digits for unit amount. Must be 2 for this test.\n"; die(1); } print __METHOD__."\n"; @@ -1294,7 +1302,7 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase $this->assertEquals(1000.123456, price2num('1 000.123456')); // Round down - $this->assertEquals(1000.12, price2num('1 000.123452', 'MT')); + $this->assertEquals(1000.12, price2num('1 000.123452', 'MT'), 'Error in round down with MT'); $this->assertEquals(1000.12345, price2num('1 000.123452', 'MU'), "Test MU"); // Round up