sindresorhus / pretty-ms

Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`
MIT License
1.08k stars 65 forks source link

Always floor time instead of rounding up #49

Closed villebro closed 4 years ago

villebro commented 4 years ago

Currently the rounding logic is slightly broken. For instance, in the unit tests, 95543ms, ie. 1 min 35 sec 543 ms rounded to 3 second decimal digits with colonNotation=true yields 1:35.580 (this should obviously be 1:35.543). I also propose that time rounding should always floor the value, not regular rounding up when the remainder is >=0.5, as otherwise the the interval between 1-2 seconds will only be 0.5 seconds long, as it is rounded to 2 seconds after as time passes 1500 ms.

This PR changes rounding so that it always rounds down. For example, with zero second precision, 1999 ms is 1 second, not 2 seconds. A new flooring function is introduced that adds an epsilon term prior to flooring to avoid floating point errors, which replaces the current logic. Without it, one the verbose unit tests for 5254ms rounded to 4 second decimal places would yield 5.2539 seconds, not 5.2540 seconds).

sindresorhus commented 4 years ago

@explodingcamera @villebro Thoughts on this?

villebro commented 4 years ago

In the absence of further comments I propose merging this, as I feel the fix is fairly critical.