@@ -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
0 commit comments