From 094677040226e9ca752017de9cba4bb0b69c6754 Mon Sep 17 00:00:00 2001 From: Jasper Briers Date: Tue, 18 Aug 2026 10:20:38 +0200 Subject: [PATCH] Fix NumberFormatter ignoring an explicit 0 decimals The check for the optional $decimals argument used a loose comparison, so an explicit 0 was treated the same as null and fell back to auto-detecting the decimals present in the input. NumberFormatter::format(12.345, 0) returned '12.345' instead of '12'. The NumberFormatterTest was never executed: it sits at the root of tests/, while phpunit.xml only registered the tests/Write and tests/Read directories. Added a 'unit' testsuite covering the root so the new cases actually run. Co-Authored-By: Claude Opus 5 (1M context) --- phpunit.xml | 5 +++++ src/NumberFormatter.php | 2 +- tests/NumberFormatterTest.php | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 08f2b37..e7cadf5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,6 +10,11 @@ + + tests + tests/Read + tests/Write + tests/Write diff --git a/src/NumberFormatter.php b/src/NumberFormatter.php index 3fe3e6f..ffe831f 100644 --- a/src/NumberFormatter.php +++ b/src/NumberFormatter.php @@ -19,7 +19,7 @@ public static function format( string $decimalSeparator = ".", string $thousandsSeparator = "" ) { - if ($decimals == null) { + if ($decimals === null) { // Get the current decimal point character according to the locale // This is needed because (string)$number uses the locale's decimal separator $locale = localeconv(); diff --git a/tests/NumberFormatterTest.php b/tests/NumberFormatterTest.php index c195c12..cdf4c23 100644 --- a/tests/NumberFormatterTest.php +++ b/tests/NumberFormatterTest.php @@ -30,6 +30,13 @@ public function formattedNumbersProvider(): array [1.236789, '1.24', 2], [1, '1.00', 2], [1.000, '1', null], + + // An explicit 0 must round to a whole number, not fall back + // to auto-detecting the decimals present in the input + [12.345, '12', 0], + [0.5, '1', 0], + [1.0, '1', 0], + [0.0, '0', 0], ]; } }