3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyでバイト数をキロバイト・メガバイト・ギガバイト・テラバイト・ペタバイト・エクサバイト・ゼタバイト・ヨタバイトに変換するサンプルコード

Last updated at Posted at 2018-01-01

ソースコード

require 'pp' # Ruby 2.5 からは明示的な pp の require が不要

def size_list(byte_size)
  # 小数点以下2桁まで求める
  {
    :B   => byte_size, # バイト (B)
    :KiB => (byte_size / (2 ** 10).to_f).round(2), # キロバイト   (kB) / キビバイト
    :MiB => (byte_size / (2 ** 20).to_f).round(2), # メガバイト   (MB) / メビバイト
    :GiB => (byte_size / (2 ** 30).to_f).round(2), # ギガバイト   (GB) / ギビバイト
    :TiB => (byte_size / (2 ** 40).to_f).round(2), # テラバイト   (TB) / テビバイト
    :PiB => (byte_size / (2 ** 50).to_f).round(2), # ペタバイト   (PB) / ペビバイト
    :EiB => (byte_size / (2 ** 60).to_f).round(2), # エクサバイト (EB) / エクスビバイト
    :ZiB => (byte_size / (2 ** 70).to_f).round(2), # ゼタバイト   (ZB) / ゼビバイト
    :YiB => (byte_size / (2 ** 80).to_f).round(2), # ヨタバイト   (YB) / ヨビバイト
  }
end

list = [
  {:bytesize => 10 **  6, :name => '100万'},
  {:bytesize => 10 **  8, :name => '1億(おく)'},
  {:bytesize => 10 ** 12, :name => '1兆(ちょう)'},
  {:bytesize => 10 ** 16, :name => '1京(けい)'},
  {:bytesize => 10 ** 20, :name => '1垓(がい)'},
  {:bytesize => 10 ** 24, :name => '1𥝱(じょ)'},
  {:bytesize => 10 ** 28, :name => '1穣(じょう)'},
]

list.each do |item|
  puts item[:name]
  pp(size_list(item[:bytesize]))
  puts
end

実行結果

100万
{:B=>1000000,
 :KiB=>976.56,
 :MiB=>0.95,
 :GiB=>0.0,
 :TiB=>0.0,
 :PiB=>0.0,
 :EiB=>0.0,
 :ZiB=>0.0,
 :YiB=>0.0}

1億(おく)
{:B=>100000000,
 :KiB=>97656.25,
 :MiB=>95.37,
 :GiB=>0.09,
 :TiB=>0.0,
 :PiB=>0.0,
 :EiB=>0.0,
 :ZiB=>0.0,
 :YiB=>0.0}

1兆(ちょう)
{:B=>1000000000000,
 :KiB=>976562500.0,
 :MiB=>953674.32,
 :GiB=>931.32,
 :TiB=>0.91,
 :PiB=>0.0,
 :EiB=>0.0,
 :ZiB=>0.0,
 :YiB=>0.0}

1京(けい)
{:B=>10000000000000000,
 :KiB=>9765625000000.0,
 :MiB=>9536743164.06,
 :GiB=>9313225.75,
 :TiB=>9094.95,
 :PiB=>8.88,
 :EiB=>0.01,
 :ZiB=>0.0,
 :YiB=>0.0}

1垓(がい)
{:B=>100000000000000000000,
 :KiB=>9.765625e+16,
 :MiB=>95367431640625.0,
 :GiB=>93132257461.55,
 :TiB=>90949470.18,
 :PiB=>88817.84,
 :EiB=>86.74,
 :ZiB=>0.08,
 :YiB=>0.0}

1𥝱(じょ)
{:B=>1000000000000000000000000,
 :KiB=>9.765625e+20,
 :MiB=>9.5367431640625e+17,
 :GiB=>931322574615478.5,
 :TiB=>909494701772.93,
 :PiB=>888178419.7,
 :EiB=>867361.74,
 :ZiB=>847.03,
 :YiB=>0.83}

1穣(じょう)
{:B=>10000000000000000000000000000,
 :KiB=>9.765625e+24,
 :MiB=>9.5367431640625e+21,
 :GiB=>9.313225746154785e+18,
 :TiB=>9.094947017729282e+15,
 :PiB=>8881784197001.25,
 :EiB=>8673617379.88,
 :ZiB=>8470329.47,
 :YiB=>8271.81}

参考資料

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?