Skip to content

Commit e83ba82

Browse files
authored
Merge pull request #133 from avoidwork/precision
Precision option
2 parents 0f4cf40 + 31d9a21 commit e83ba82

11 files changed

+1635
-1419
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ _*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), de
3535
### pad
3636
_*(boolean)*_ Decimal place end padding, default is `false`
3737

38+
### precision
39+
_*(number)*_ Sets precision of numerical output, default is `0`
40+
3841
### round
3942
_*(number)*_ Decimal place, default is `2`
4043

44+
### roundingMethod
45+
_*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`
46+
4147
### separator
4248
_*(string)*_ Decimal separator character, default is `.`
4349

@@ -53,9 +59,6 @@ _*(object)*_ Dictionary of SI/JEDEC/IEC symbols to replace for localization, def
5359
### unix
5460
_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false`
5561

56-
### roundingMethod
57-
_*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`
58-
5962
## Examples
6063

6164
```javascript

lib/filesize.es6.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
function filesize (arg, descriptor = {}) {
4343
let result = [],
4444
val = 0,
45-
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc;
45+
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;
4646

4747
if (isNaN(arg)) {
4848
throw new TypeError("Invalid number");
@@ -67,6 +67,7 @@
6767
num = Number(arg);
6868
neg = num < 0;
6969
ceil = base > 2 ? 1000 : 1024;
70+
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;
7071

7172
// Flipping a negative number to determine the size
7273
if (neg) {
@@ -84,6 +85,10 @@
8485

8586
// Exceeding supported length, time to reduce & multiply
8687
if (e > 8) {
88+
if (precision > 0) {
89+
precision += 8 - e;
90+
}
91+
8792
e = 8;
8893
}
8994

@@ -132,6 +137,11 @@
132137
result[0] = -result[0];
133138
}
134139

140+
// Setting optional precision
141+
if (precision > 0) {
142+
result[0] = result[0].toPrecision(precision);
143+
}
144+
135145
// Applying custom symbol
136146
result[1] = symbols[result[1]] || result[1];
137147

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
spacer,
6363
standard,
6464
symbols,
65-
roundingFunc;
65+
roundingFunc,
66+
precision;
6667

6768
if (isNaN(arg)) {
6869
throw new TypeError("Invalid number");
@@ -86,7 +87,8 @@
8687
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
8788
num = Number(arg);
8889
neg = num < 0;
89-
ceil = base > 2 ? 1000 : 1024; // Flipping a negative number to determine the size
90+
ceil = base > 2 ? 1000 : 1024;
91+
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; // Flipping a negative number to determine the size
9092

9193
if (neg) {
9294
num = -num;
@@ -103,6 +105,10 @@
103105

104106

105107
if (e > 8) {
108+
if (precision > 0) {
109+
precision += 8 - e;
110+
}
111+
106112
e = 8;
107113
}
108114

@@ -149,6 +155,11 @@
149155

150156
if (neg) {
151157
result[0] = -result[0];
158+
} // Setting optional precision
159+
160+
161+
if (precision > 0) {
162+
result[0] = result[0].toPrecision(precision);
152163
} // Applying custom symbol
153164

154165

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)