Fix byte formatting abbreviations to use established standards #49115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In #48845, a
Number
class was added, following a request to add a file size formatting function to Laravel. After the ultimate merge, the byte formatting functionNumber::fileSize()
uses a custom abbreviation scheme. I want to cite my two comments in the mentioned pull request:Oct 30
Oct 31
To put the above comments into current context, Laravel 10.x since ships with a
Number::fileSize()
method that reports metric abbreviations, while it performs calculations using multiples of 1024. This leads to erroneous results when formatting relatively large numbers, e.g. an amount of10^13
bytes is reported as10 TB
while it surely should be11 TB
, according to the definition ofT = 10^12
. A more detailed explanation of related errors can be found on wikipedia.Another way to put it: currently this method reports metric units, but calculates quantities in a non-metric fashion.
Reports of byte quantities using multiples of 1024 do have their place within computing, but these are primarily in technical details like amount of RAM, storage disk space or CPU caches, where the hardware is built in a binary fashion by nature. A file size is not binary by nature, as a file can be any number of bytes large, not limited to only being equal to multiples of 2. Hence, it is not necessary nor logical to use multiples of 1024 in file size reporting.
This leads me to the conclusion that the most enduser-friendly thing to do here, is to use multiples of 1000 instead of 1024 to calculate the (abbreviated) units:
MB
,GB
,TB
, ... is in line with the metric systemIn the rare circumstance one needs to report figures about hardware properties, I propose developers create their own method that reports byte quantities in binary units (kibi, mebi, ...), along with calculations using multiples of 1024.
To sum up, in this PR I propose the following 2 changes to fully comply with the metric system in order to use the metric prefixes:
k
for kilo instead ofK
.If this PR does not get merged, I would advise to update the docs on this method to warn developers that the used abbreviations follow a custom naming scheme that does not follow an established standard.