Ruby 获取准确的文件大小(以MB为单位)?

Ruby 获取准确的文件大小(以MB为单位)?,ruby,Ruby,如何获得准确的文件大小(MB)?我试过这个: compressed_file_size = File.size("Compressed/#{project}.tar.bz2") / 1024000 puts "file size is #{compressed_file_size} MB" 但是它将0.9的值切掉,显示的是2MB,而不是2.9MB。你在做整数除法(去掉小数部分)。尝试除以1024000.0,这样ruby就知道你想做浮点运算。尝试: compressed_file_size =

如何获得准确的文件大小(MB)?我试过这个:

compressed_file_size = File.size("Compressed/#{project}.tar.bz2") / 1024000

puts "file size is #{compressed_file_size} MB"

但是它将0.9的值切掉,显示的是2MB,而不是2.9MB。你在做整数除法(去掉小数部分)。尝试除以1024000.0,这样ruby就知道你想做浮点运算。

尝试:

compressed_file_size = File.size("Compressed/#{project}.tar.bz2").to_f / 1024000
尝试:

一艘班轮:

compressed_file_size = '%.2f' % (File.size("Compressed/#{project}.tar.bz2").to_f / 2**20)
或:


有关
%
-字符串运算符的详细信息:



顺便说一句:如果我使用base2计算,我更喜欢“MiB”而不是“MB”(请参见:)

您可能会发现格式化函数很有用(),下面是我的示例

def format_mb(size)
  conv = [ 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb' ];
  scale = 1024;

  ndx=1
  if( size < 2*(scale**ndx)  ) then
    return "#{(size)} #{conv[ndx-1]}"
  end
  size=size.to_f
  [2,3,4,5,6,7].each do |ndx|
    if( size < 2*(scale**ndx)  ) then
      return "#{'%.3f' % (size/(scale**(ndx-1)))} #{conv[ndx-1]}"
    end
  end
  ndx=7
  return "#{'%.3f' % (size/(scale**(ndx-1)))} #{conv[ndx-1]}"
end
产生,

size 1 -> 1 b
size 2 -> 2 b
size 3 -> 3 b
size 500 -> 500 b
size 1000 -> 1000 b
size 1024 -> 1024 b
size 3000 -> 2.930 kb
size 99999 -> 97.655 kb
size 999999 -> 976.562 kb
size 999999999 -> 953.674 mb
size 9999999999 -> 9.313 gb
size 999999999999 -> 931.323 gb
size 99999999999999 -> 90.949 tb
size 3333333333333333 -> 2.961 pb
size 555555555555555555555 -> 481.868 eb

我甚至不使用Ruby,这正是我要建议的。这也是。它给了我2.8679921875 MB,我只想把2.86 MB从浮点对整数的问题中分离出来,1024000真的是你想要的常数吗?通常MB是2^20,也就是1048576。谢谢你的提示。我在我的代码中修复了这一点。根据您想要的功能有多全面,Rails的源代码
ActionView::Helpers::NumberHelper#number_to_human_size
是一个很好的参考实现。知道这是很古老的,但现在发生的是你被一个整数除,它返回整数,也就是。把号码切掉。如果在末尾添加一个
.0
,它将使用浮点进行计算。所以:
压缩文件大小=…../1024000.0
应该为您提供浮点数。您可能需要在
文件.size(…)
上添加一个
。这给了我2.8679921875 MB,我只需要2.86 MB。您可以使用压缩文件大小。舍入(2)或.2f'%compressed\u File\u size在输出时,我没有得到.2f'%compressed\u File\u size在输出时。请澄清.puts“文件大小为#{.2f'%compressed\u file\u size}MB”尝试:
formatted\u file\u size='%.2f'%compressed\u file\u size
——格式字符串中缺少“%”。有关
%
-字符串运算符()
def format_mb(size)
  conv = [ 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb' ];
  scale = 1024;

  ndx=1
  if( size < 2*(scale**ndx)  ) then
    return "#{(size)} #{conv[ndx-1]}"
  end
  size=size.to_f
  [2,3,4,5,6,7].each do |ndx|
    if( size < 2*(scale**ndx)  ) then
      return "#{'%.3f' % (size/(scale**(ndx-1)))} #{conv[ndx-1]}"
    end
  end
  ndx=7
  return "#{'%.3f' % (size/(scale**(ndx-1)))} #{conv[ndx-1]}"
end
tries = [ 1,2,3,500,1000,1024,3000,99999,999999,999999999,9999999999,999999999999,99999999999999,3333333333333333,555555555555555555555]

tries.each { |x|
  print "size #{x} -> #{format_mb(x)}\n"
}
size 1 -> 1 b
size 2 -> 2 b
size 3 -> 3 b
size 500 -> 500 b
size 1000 -> 1000 b
size 1024 -> 1024 b
size 3000 -> 2.930 kb
size 99999 -> 97.655 kb
size 999999 -> 976.562 kb
size 999999999 -> 953.674 mb
size 9999999999 -> 9.313 gb
size 999999999999 -> 931.323 gb
size 99999999999999 -> 90.949 tb
size 3333333333333333 -> 2.961 pb
size 555555555555555555555 -> 481.868 eb