Skip to content

Commit 1638bd2

Browse files
authored
Fixing how 'precision' adjusts the exponent if there's digits (#202)
1 parent 091c8e1 commit 1638bd2

8 files changed

Lines changed: 68 additions & 28 deletions

File tree

dist/filesize.cjs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ function filesize (arg, {
167167
// Zero is now a special case because bytes divide by 1
168168
if (num === 0) {
169169
result[0] = 0;
170+
171+
if (precision > 0) {
172+
result[0] = result[0].toPrecision(precision);
173+
}
174+
170175
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
171176
} else {
172177
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
@@ -188,6 +193,17 @@ function filesize (arg, {
188193
e++;
189194
}
190195

196+
// Setting optional precision
197+
if (precision > 0) {
198+
result[0] = result[0].toPrecision(precision);
199+
const [ newResult, newExponent ] = result[0].split(E);
200+
result[0] = newResult;
201+
202+
if (newExponent !== undefined) {
203+
e += parseInt(newExponent, 10);
204+
}
205+
}
206+
191207
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
192208
}
193209

@@ -196,12 +212,6 @@ function filesize (arg, {
196212
result[0] = -result[0];
197213
}
198214

199-
// Setting optional precision
200-
if (precision > 0) {
201-
result[0] = result[0].toPrecision(precision);
202-
result[0] = result[0].split(E)[0];
203-
}
204-
205215
// Applying custom symbol
206216
result[1] = symbols[result[1]] || result[1];
207217

dist/filesize.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ function filesize (arg, {
163163
// Zero is now a special case because bytes divide by 1
164164
if (num === 0) {
165165
result[0] = 0;
166+
167+
if (precision > 0) {
168+
result[0] = result[0].toPrecision(precision);
169+
}
170+
166171
u = result[1] = STRINGS.symbol[standard][bits ? BITS : BYTES][e];
167172
} else {
168173
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
@@ -184,6 +189,17 @@ function filesize (arg, {
184189
e++;
185190
}
186191

192+
// Setting optional precision
193+
if (precision > 0) {
194+
result[0] = result[0].toPrecision(precision);
195+
const [ newResult, newExponent ] = result[0].split(E);
196+
result[0] = newResult;
197+
198+
if (newExponent !== undefined) {
199+
e += parseInt(newExponent, 10);
200+
}
201+
}
202+
187203
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : STRINGS.symbol[standard][bits ? BITS : BYTES][e];
188204
}
189205

@@ -192,12 +208,6 @@ function filesize (arg, {
192208
result[0] = -result[0];
193209
}
194210

195-
// Setting optional precision
196-
if (precision > 0) {
197-
result[0] = result[0].toPrecision(precision);
198-
result[0] = result[0].split(E)[0];
199-
}
200-
201211
// Applying custom symbol
202212
result[1] = symbols[result[1]] || result[1];
203213

dist/filesize.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)