Skip to content

Commit d589a0e

Browse files
committed
#191 Bugfix: round may result in improper coefficient
1 parent f842264 commit d589a0e

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

big.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,18 @@
230230
rm === 3 && (more || !!xc[0]);
231231

232232
// Remove any digits after the required precision.
233-
xc.length = sd--;
233+
xc.length = sd;
234234

235235
// Round up?
236236
if (more) {
237237

238238
// Rounding up may mean the previous digit has to be rounded up.
239-
for (; ++xc[sd] > 9;) {
239+
for (; ++xc[--sd] > 9;) {
240240
xc[sd] = 0;
241-
if (!sd--) {
241+
if (sd === 0) {
242242
++x.e;
243243
xc.unshift(1);
244+
break;
244245
}
245246
}
246247
}

big.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,18 @@ function round(x, sd, rm, more) {
227227
rm === 3 && (more || !!xc[0]);
228228

229229
// Remove any digits after the required precision.
230-
xc.length = sd--;
230+
xc.length = sd;
231231

232232
// Round up?
233233
if (more) {
234234

235235
// Rounding up may mean the previous digit has to be rounded up.
236-
for (; ++xc[sd] > 9;) {
236+
for (; ++xc[--sd] > 9;) {
237237
xc[sd] = 0;
238-
if (!sd--) {
238+
if (sd === 0) {
239239
++x.e;
240240
xc.unshift(1);
241+
break;
241242
}
242243
}
243244
}

test/methods/round.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5286,4 +5286,7 @@ test('round', function () {
52865286
t('0', '98765000', -10, 1);
52875287
t('0', '98765000', -10, 2);
52885288
t('10000000000', '98765000', -10, 3);
5289+
5290+
// #191
5291+
test.isTrue(new Big('9.9').round().c['-1'] === u);
52895292
});

0 commit comments

Comments
 (0)