Skip to content

Commit f2acf64

Browse files
add roundingMethod option
1 parent e1448a6 commit f2acf64

10 files changed

+49
-10
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ _*(object)*_ Dictionary of SI/JEDEC/IEC symbols to replace for localization, def
5353
### unix
5454
_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`
5555

56+
### roundingMethod
57+
_*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`
58+
5659
## Examples
5760

5861
```javascript

filesize.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ declare namespace Filesize {
9494
* Enables unix style human readable output, e.g ls -lh, default is false
9595
*/
9696
unix?: boolean;
97+
/**
98+
* Rounding method, can be round, floor, or ceil, default is round
99+
*/
100+
roundingMethod?: "round" | "floor" | "ceil";
97101
}
98102

99103
interface Filesize {

lib/filesize.es6.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
fullform = {
2626
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
2727
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
28+
},
29+
roundingFuncs = {
30+
floor: Math.floor,
31+
ceil: Math.ceil
2832
};
2933

3034
/**
@@ -38,7 +42,7 @@
3842
function filesize (arg, descriptor = {}) {
3943
let result = [],
4044
val = 0,
41-
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols;
45+
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc;
4246

4347
if (isNaN(arg)) {
4448
throw new TypeError("Invalid number");
@@ -59,6 +63,7 @@
5963
full = descriptor.fullform === true;
6064
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
6165
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
66+
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
6267
num = Number(arg);
6368
neg = num < 0;
6469
ceil = base > 2 ? 1000 : 1024;
@@ -102,7 +107,8 @@
102107
}
103108
}
104109

105-
result[0] = Number(val.toFixed(e > 0 ? round : 0));
110+
const p = Math.pow(10, e > 0 ? round : 0);
111+
result[0] = roundingFunc(val * p) / p;
106112

107113
if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
108114
result[0] = 1;

lib/filesize.es6.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.es6.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
fullform = {
2626
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
2727
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
28+
},
29+
roundingFuncs = {
30+
floor: Math.floor,
31+
ceil: Math.ceil
2832
};
2933
/**
3034
* filesize
@@ -57,7 +61,8 @@
5761
separator,
5862
spacer,
5963
standard,
60-
symbols;
64+
symbols,
65+
roundingFunc;
6166

6267
if (isNaN(arg)) {
6368
throw new TypeError("Invalid number");
@@ -78,6 +83,7 @@
7883
full = descriptor.fullform === true;
7984
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
8085
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
86+
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
8187
num = Number(arg);
8288
neg = num < 0;
8389
ceil = base > 2 ? 1000 : 1024; // Flipping a negative number to determine the size
@@ -120,7 +126,8 @@
120126
}
121127
}
122128

123-
result[0] = Number(val.toFixed(e > 0 ? round : 0));
129+
var p = Math.pow(10, e > 0 ? round : 0);
130+
result[0] = roundingFunc(val * p) / p;
124131

125132
if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
126133
result[0] = 1;

lib/filesize.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)