Closed timkendrick closed 8 years ago
Update: I've added optional support for a fixed number of decimal places, as follows:
bytes(1024, {precision: 3, fixed: true});
// output: '1.000kB'
...this should allow for use-cases such as #25.
I was just looking for something like this! My use-case is the fixed
one.
@dougwilson @tj – any chance somebody could take a look at this? Would be great to get it merged!
Sorry @timkendrick , I'll take a look :)
Sorry it took so long for me to look at this PR! There is an issue with the name of the argument, precision
. It seems too easily confused with the existing API in JavaScript Number.prototype.toPrecision
, which does the rounding per definition, which does not match what is being done here.
For example, one would expect bytes(1024 * 1.12345, {precision: 3});
to be 1.12kB, not 1.123kB, since the first matches toPrecision
and the dictionary definition of precision.
No problem – that makes sense. Any suggestions for an alternate name?
I couldn't think of any name at the time I wrote that, but maybe decimalPlaces
?
Fine by me!
I just merged up the decimalPlaces
portion of the PR. I didn't want to tie up the PR with two changes at the same time, so one didn't block the other. I'm looking over the fixed portion of the changeset now.
Right now still debating between the option being fixed
or something like fixedDecimals
or decimalsFixed
or something, IDK.
Hi @timkendrick , would you mind re-issuing the fixed
part of this PR as another PR?
This PR allows you to specify the maximum number of decimal places to include in the output of
bytes.format()
.Examples:
...note that this is a maximum number of decimal places (rather than a fixed number of decimal places) – i.e. trailing zeroes will be hidden:
Let me know if there's any changes I can make to help get this merged!