Skip to content

Commit 528f53e

Browse files
committed
merge #57: fix rounding issues
2 parents 13e3ec2 + b3ee960 commit 528f53e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/Formatters/Number/NumberFormatter.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,29 @@ private function applyMaximumFractionDigits(Options $o, string $int, string $fra
144144
assert($maximumFractionDigits !== null);
145145

146146
if ($maximumFractionDigits <= 0) {
147-
$intWithoutLast = substr($int, 0, strlen($int) - 1);
148-
$intLastDigit = (int) substr($int, strlen($int) - 1);
149-
150147
if ((int) substr($frac, 0, 1) >= 5) {
151-
$intLastDigit++;
148+
$int++;
152149
}
153150

154-
return [$intWithoutLast . $intLastDigit, ''];
151+
return [(string) $int, ''];
155152
}
156153

157154
if (strlen($frac) <= $maximumFractionDigits) {
158155
return [$int, $frac];
159156
}
160157

161-
$withoutLast = substr($frac, 0, $maximumFractionDigits - 1);
162-
$lastDigit = (int) substr($frac, $maximumFractionDigits - 1, 1);
158+
$paddingZeros = substr($frac, 0, strspn($frac, '0'));
159+
$trimmed = (int) substr($frac, 0, $maximumFractionDigits);
163160

164161
if ((int) substr($frac, $maximumFractionDigits, 1) >= 5) {
165-
$lastDigit++;
162+
$trimmed++;
163+
}
164+
165+
if (strlen((string) $trimmed) > $maximumFractionDigits) {
166+
return [(string) ++$int, ''];
166167
}
167168

168-
return [$int, $withoutLast . $lastDigit];
169+
return [$int, rtrim($paddingZeros . $trimmed, '0')];
169170
}
170171

171172
private function applyMaximumSignificantDigits(Options $o, string $int): string
@@ -177,15 +178,14 @@ private function applyMaximumSignificantDigits(Options $o, string $int): string
177178
return $int;
178179
}
179180

180-
$withoutLast = substr($int, 0, $o->maximumSignificantDigits - 1);
181-
$lastDigit = (int) substr($int, $o->maximumSignificantDigits - 1, 1);
182-
$padding = str_repeat('0', strlen($int) - $o->maximumSignificantDigits);
181+
$trimmed = (int) substr($int, 0, $o->maximumSignificantDigits);
182+
$paddingZeros = str_repeat('0', strlen($int) - $o->maximumSignificantDigits);
183183

184184
if ((int) substr($int, $o->maximumSignificantDigits, 1) >= 5) {
185-
$lastDigit++;
185+
$trimmed++;
186186
}
187187

188-
return $withoutLast . $lastDigit . $padding;
188+
return $trimmed . $paddingZeros;
189189
}
190190

191191
private function applyMinimumIntegerDigits(Options $o, string $int): string

tests/Formatters/Number/DecimalFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public static function provideDecimalCases(): Generator
5252
[12.345, ['maximumSignificantDigits' => 4]],
5353
[12345, ['maximumSignificantDigits' => 4]],
5454
[45.67, ['maximumSignificantDigits' => 2]],
55+
[5962399.87, ['maximumSignificantDigits' => 3]],
56+
[46538.069971871, ['maxFractionDigits' => 4]],
57+
[46538.999971871, ['maxFractionDigits' => 4]],
5558
] as $case) {
5659
foreach (self::provideLocales() as $locale) {
5760
yield [$locale, ...$case];

0 commit comments

Comments
 (0)