Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@ class NumberFormatter
* @param string $thousandsSeparator
* @return void
*/
public static function format($number, ?int $decimals = null, string $decimalSeparator = '.', string $thousandsSeparator = '')
{
public static function format(
$number,
?int $decimals = null,
string $decimalSeparator = ".",
string $thousandsSeparator = ""
) {
if ($decimals == null) {

$decimalPoint = '.';
if(!is_float($number)) {
// Convert to string to detect decimals
// Get the current decimal point character according to the locale
$locale = localeconv();
$decimalPoint = $locale['decimal_point'] ?? '.';
}
// Get the current decimal point character according to the locale
// This is needed because (string)$number uses the locale's decimal separator
$locale = localeconv();
$decimalPoint = $locale["decimal_point"] ?? ".";

// Convert to string to detect decimals
$parts = explode($decimalPoint, (string)$number);
$parts = explode($decimalPoint, (string) $number);

// Count decimals, if any
$decimals = isset($parts[1]) ? strlen(rtrim($parts[1], '0')) : 0;
$decimals = isset($parts[1]) ? strlen(rtrim($parts[1], "0")) : 0;
}

return number_format($number, $decimals, $decimalSeparator, $thousandsSeparator);
$value = number_format(
$number,
$decimals,
$decimalSeparator,
$thousandsSeparator
);

return $value;
}
}