Skip to content

Commit fb3b237

Browse files
authored
Merge pull request #157 from num-num/refactor/number-formatter-cleanup
2 parents e691b57 + 9e7e36b commit fb3b237

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/NumberFormatter.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,32 @@ class NumberFormatter
1313
* @param string $thousandsSeparator
1414
* @return void
1515
*/
16-
public static function format($number, ?int $decimals = null, string $decimalSeparator = '.', string $thousandsSeparator = '')
17-
{
16+
public static function format(
17+
$number,
18+
?int $decimals = null,
19+
string $decimalSeparator = ".",
20+
string $thousandsSeparator = ""
21+
) {
1822
if ($decimals == null) {
19-
20-
$decimalPoint = '.';
21-
if(!is_float($number)) {
22-
// Convert to string to detect decimals
23-
// Get the current decimal point character according to the locale
24-
$locale = localeconv();
25-
$decimalPoint = $locale['decimal_point'] ?? '.';
26-
}
23+
// Get the current decimal point character according to the locale
24+
// This is needed because (string)$number uses the locale's decimal separator
25+
$locale = localeconv();
26+
$decimalPoint = $locale["decimal_point"] ?? ".";
2727

2828
// Convert to string to detect decimals
29-
$parts = explode($decimalPoint, (string)$number);
29+
$parts = explode($decimalPoint, (string) $number);
3030

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

35-
return number_format($number, $decimals, $decimalSeparator, $thousandsSeparator);
35+
$value = number_format(
36+
$number,
37+
$decimals,
38+
$decimalSeparator,
39+
$thousandsSeparator
40+
);
41+
42+
return $value;
3643
}
3744
}

0 commit comments

Comments
 (0)