Number_to_human_size

Hi all.

I have noticed some questionable behaviour when converting byte sizes
into a human-readable form.

$ruby script\console
Loading development environment (Rails 2.3.8)
>> include ActionView::Helpers::NumberHelper
=> Object
>> number_to_human_size(1000)
=> "1000 Bytes"             # I wanted "1 kB"
>> number_to_human_size(1000000)
=> "976.6 KB"             # I wanted "1 MB"
>>
  1. It’s using the wrong prefix for the multiplier, or using the wrong
    multiplier for the prefix.
    (a) If using SI / metric prefixes, the multiplier should be 1000
    (b) If using a multiplier of 1024, the prefixes should be the binary
    style (ki,Mi,Gi,…)
    http://en.wikipedia.org/wiki/Binary_prefix

Mixing these up isn’t great but a lot of people do get it wrong, and
it’s a hard habit to break. Ideally I suppose the method could have
a :format option for the three types of behaviour:

:metric Use 1000 and metric prefixes
:binary Use 1024 and binary prefixes
:broken Use 1000 and binary prefixes

And default to :binary, perhaps.

  1. The “k” for “kilo” should be in lowercase.
    http://en.wikipedia.org/wiki/SI_prefix

TX

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs