Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision option #133

Merged
merged 2 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ _*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), de
### pad
_*(boolean)*_ Decimal place end padding, default is `false`

### precision
_*(number)*_ Sets precision of numerical output, default is `0`

### round
_*(number)*_ Decimal place, default is `2`

### roundingMethod
_*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`

### separator
_*(string)*_ Decimal separator character, default is `.`

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

### roundingMethod
_*(string)*_ Rounding method, can be `round`, `floor`, or `ceil`, default is `round`

## Examples

```javascript
Expand Down
12 changes: 11 additions & 1 deletion lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;

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

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

// Exceeding supported length, time to reduce & multiply
if (e > 8) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

Expand Down Expand Up @@ -132,6 +137,11 @@
result[0] = -result[0];
}

// Setting optional precision
if (precision > 0) {
result[0] = result[0].toPrecision(precision);
}

// Applying custom symbol
result[1] = symbols[result[1]] || result[1];

Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js.map

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
spacer,
standard,
symbols,
roundingFunc;
roundingFunc,
precision;

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

if (neg) {
num = -num;
Expand All @@ -103,6 +105,10 @@


if (e > 8) {
if (precision > 0) {
precision += 8 - e;
}

e = 8;
}

Expand Down Expand Up @@ -149,6 +155,11 @@

if (neg) {
result[0] = -result[0];
} // Setting optional precision


if (precision > 0) {
result[0] = result[0].toPrecision(precision);
} // Applying custom symbol


Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading