Help us understand the problem. What is going on with this article?

ファイルサイズ表示に役立つgem filesize

More than 1 year has passed since last update.

filesizeというgemを使ったので備忘録

https://github.com/dominikh/filesize

148MBって何バイト?
13982730000バイトって何GB?

こんなときに使えるgem

  • 148MBをバイト単位に変換

    Filesize.from("148MB").to_i
    => 148000000
    
  • 13982730000バイトをGB単位に変換

    Filesize.from("13982730000 B").to_f('GB')
    => 13.98273
    

単位を指定せず、見やすい単位を自動で選んでもらうこともできる

  • 13982730000バイトを見やすい単位に変換
    Filesize.from("13982730000 B").pretty
    => "13.02 GiB"
    

prettyを使うと、単位がKiB、MiB、GiBとなる

表示はMBやGBにしたいがどうしようと悩んでいたところ、先頭に書いたURLの内容を読んでいると以下の記載

(Filesize.from("1400 MB") + Filesize.from("1400 MiB")).pretty # => "2.87 GB"
(Filesize.from("1400 MiB") + Filesize.from("1400 MB")).pretty # => "2.67 GiB"

ということはこれでいけるのでは?と試したのがこちら

(Filesize.from("0 KB") + Filesize.from("13982730000 B")).pretty
=> "13.98 GB"

GB表示にすることができたが、注意点がいくつか

  • 小数第2位へ丸められる
  • 1GiB = 1024 * 1024 * 1024 byteだが、1GB = 1000 * 1000 * 1000 byteで計算される
marlion7
sorich
SORICHはWebシステム開発を主軸に、デザイン・Web制作・アプリ開発・IoTまで、クライアントの幅広いニーズに対応する技術者集団です。
https://www.sorich.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした