cli icon indicating copy to clipboard operation
cli copied to clipboard

fix(utils): correct byte formatting boundaries for rounding

Open watilde opened this issue 3 weeks ago • 3 comments

WHY/WHAT

Fixed non-intuitive unit rounding for byte formatting. Previously, values close to a unit boundary (e.g. 999,999 B) displayed incorrectly as the smaller unit. We adjusted the conversion threshold (e.g. to 999,950 B for MB) to leverage standard toFixed(1) rounding, ensuring values that are almost 1.0 of the next unit are correctly displayed as 1.0 MB, prioritizing a better user experience.

watilde avatar Dec 08 '25 22:12 watilde

Why was this only done for two of the units?

wraithgar avatar Dec 09 '25 19:12 wraithgar

This fix prevents inconsistencies caused by toFixed(1) rounding. e.g. preventing 999.95 from displaying as "1000.0".

Why was this only done for two of the units?

  • kB → MB / MB → GB (Fixed): Previously, 999,950 bytes (999.95 kB) rounded up to 1000.0 kB. I lowered the threshold to ensure these values switch to the next unit (e.g., 1.0 MB) instead.
  • Bytes → kB (No fix): Bytes are integers (999 B → 1000 B), so fractional rounding issues do not occur.
  • GB → TB (No fix): The current logic only supports up to GB, so no upper transition exists.

watilde avatar Dec 10 '25 14:12 watilde

rebase on the latest and added the tests to tar as suggested

watilde avatar Dec 10 '25 14:12 watilde