Class: Integer

Inherits:
Object show all
Defined in:
lib/unixcmd/aux.rb

Instance Method Summary collapse

Instance Method Details

#to_szstrObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/unixcmd/aux.rb', line 52

def to_szstr 
    number = self
    if number < 1024
        exponent = 0
    else
        max_exp  = UNITS.size - 1

        exponent = ( Math.log(number) / Math.log(1024) ).to_i # convert to base
        exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit

        number /= 1024 ** exponent
    end

    "#{number}" + (exponent > 0 ? " #{UNITS[exponent-1]}" : '')
end