diff --git a/config/config.php b/config/config.php index 137a7d0..49ff845 100644 --- a/config/config.php +++ b/config/config.php @@ -10,9 +10,7 @@ 'decimals' => env('LARAVEL_CART_DECIMALS', 0), - 'dec_point' => env('LARAVEL_CART_DEC_POINT', '.'), - - 'thousands_sep' => env('LARAVEL_CART_THOUSANDS_SEP', ','), + 'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'), /* * --------------------------------------------------------------- diff --git a/src/Helpers/Helpers.php b/src/Helpers/Helpers.php index 5bd14ea..d902cbc 100644 --- a/src/Helpers/Helpers.php +++ b/src/Helpers/Helpers.php @@ -5,15 +5,25 @@ class Helpers { /** - * normalize price - * - * @return float + * Normalize prices */ - public static function normalizePrice($price) + public static function normalizePrice($price): float { return (is_string($price)) ? floatval($price) : $price; } + /** + * Get the rounding mode + */ + public static function roundMode($config): int + { + if (isset($config['round_mode']) && $config['round_mode'] == 'up') { + return PHP_ROUND_HALF_UP; + } + + return PHP_ROUND_HALF_DOWN; + } + /** * check if array is multi dimensional array * This will only check the first element of the array if it is still an array @@ -61,7 +71,7 @@ public static function formatValue($value, $formatNumber, $config) } if ($formatNumber || $config['format_numbers']) { - return floatval(number_format($value, $config['decimals'], $config['dec_point'], $config['thousands_sep'])); + return round($value, $config['decimals'], self::roundMode($config)); } else { return floatval($value); }