move to round() instead of number_format()

This commit is contained in:
Joel Male
2024-08-16 15:06:33 +10:00
parent 24f64e712c
commit d48f54cf16
2 changed files with 16 additions and 8 deletions
+1 -3
View File
@@ -10,9 +10,7 @@
'decimals' => env('LARAVEL_CART_DECIMALS', 0), 'decimals' => env('LARAVEL_CART_DECIMALS', 0),
'dec_point' => env('LARAVEL_CART_DEC_POINT', '.'), 'round_mode' => env('LARAVEL_CART_ROUND_MODE', 'down'),
'thousands_sep' => env('LARAVEL_CART_THOUSANDS_SEP', ','),
/* /*
* --------------------------------------------------------------- * ---------------------------------------------------------------
+15 -5
View File
@@ -5,15 +5,25 @@
class Helpers class Helpers
{ {
/** /**
* normalize price * Normalize prices
*
* @return float
*/ */
public static function normalizePrice($price) public static function normalizePrice($price): float
{ {
return (is_string($price)) ? floatval($price) : $price; 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 * check if array is multi dimensional array
* This will only check the first element of the array if it is still an 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']) { 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 { } else {
return floatval($value); return floatval($value);
} }