Forum: Ruby on Rails Convert bytes to kb or mb in words

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-05-29 08:27
(Received via mailing list)
Using File.size(myfile) I can get the size of the uploaded file in
_bytes_.

Does anybody know of a function to convert bytes into a more human
readable
format?

If I had a wish list for a "file_size_in_words()" function, it would do
this:

10752 bytes becomes "10.5 Kilobytes".
2213814 bytes becomes "2.1 Megabytes".
238 bytes becomes "Less Than 1 Kilobyte".

Maybe I need to learn how to write a plug-in and figure it out! Or maybe
somebody has done this and can point me in the right direction.

Thanks!

Jeff
66436ae6502afa76c02fb18f943c69e8?d=identicon&s=25 Phillip Kast (Guest)
on 2006-05-29 08:46
(Received via mailing list)
Jeff Ward wrote:
> Using File.size(myfile) I can get the size of the uploaded file in
> _bytes_.
>
> Does anybody know of a function to convert bytes into a more human
> readable format?
Perhaps the human_size helper is what you're looking for?
http://api.rubyonrails.org/classes/ActionView/Help...

    Loading development environment.
     >> include ActionView::Helpers::NumberHelper
    => Object
     >> i = 123456
    => 123456
     >> human_size i
    => "120.6 KB"

hope that helps,
guess you'll have to focus your plugin energies upwards and onwards ;)

-phil
3036c74b9c278dc525af5692080a6d80?d=identicon&s=25 Jeff Ward (Guest)
on 2006-05-29 08:49
(Received via mailing list)
Yes! This will do the trick! Thanks.
This topic is locked and can not be replied to.