# Number\_to\_human\_size

**URL:** <https://www.ruby-forum.com/t/number-to-human-size/191945>\
**Category:** Rails\
**Created:** [July 21, 2010, 5:48am UTC](https://www.ruby-forum.com/t/number-to-human-size/191945 "2010-07-21T05:48:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![trejkaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@trejkaz](https://www.ruby-forum.com/u/trejkaz)\
**Post date:** [July 21, 2010, 5:48am UTC](https://www.ruby-forum.com/t/number-to-human-size/191945/1 "2010-07-21T05:48:41Z")

</div>

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,…)  
[Binary prefix - Wikipedia](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.  
[Metric prefix - Wikipedia](http://en.wikipedia.org/wiki/SI_prefix)

TX
