openvpn-status-web/lib/openvpn-status-web/int_patch.rb

17 lines
273 B
Ruby

class Integer
def as_bytes
return "1 Byte" if self == 1
label = ["Bytes", "KiB", "MiB", "GiB", "TiB"]
i = 0
num = self.to_f
while num >= 1024 do
num = num / 1024
i += 1
end
"#{format('%.2f', num)} #{label[i]}"
end
end