Module: SI

Included in:
Bignum, Fixnum, Float, Rational
Defined in:
lib/si/module.rb,
lib/si/version.rb,
lib/si/constants.rb

Constant Summary collapse

VERSION =
"0.1.4"
PREFIXES =
{
  -8 => 'y',
  -7 => 'z',
  -6 => 'a',
  -5 => 'f',
  -4 => 'p',
  -3 => 'n',
  -2 => 'μ',
  -1 => 'm',
   0 => '',
   1 => 'k',
   2 => 'M',
   3 => 'G',
   4 => 'T',
   5 => 'P',
   6 => 'E',
   7 => 'Z',
   8 => 'Y'
}
DEFAULT =
{
  :length  =>    3,
  :base    => 1000,
  :min_exp =>   -8,
  :max_exp =>    8,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(num, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/si/module.rb', line 3

def convert num, options = {}
  options = { :length => options } if options.is_a?(Fixnum)
  options = DEFAULT.merge(options)
  length,
  min_exp,
  max_exp = options.values_at(:length, :min_exp, :max_exp)
  raise ArgumentError.new("Invalid length") if length < 2
  return num.is_a?(Fixnum) ? '0' : "0.#{'0' * (length - 1)}" if num == 0

  base    = options[:base].to_f
  minus   = num < 0 ? '-' : ''
  nump    = num.abs

  PREFIXES.keys.sort.reverse.select { |exp| (min_exp..max_exp).include? exp }.each do |exp|
    denom = base ** exp
    if nump >= denom || exp == min_exp
      val = nump / denom
      val = SI.round val, [length - val.to_i.to_s.length, 0].max
      val = val.to_i if exp == 0 && num.is_a?(Fixnum)
      val = val.to_s.ljust(length + 1, '0') if val.is_a?(Float)

      return "#{minus}#{val}#{PREFIXES[exp]}"
    end
  end

  nil
end

.revert(str, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/si/module.rb', line 31

def revert str, options = {}
  options = Hash[ DEFAULT.select { |k, v| k == :base } ].merge(options)
  pair    = PREFIXES.to_a.find { |k, v| !v.empty? && str =~ /[0-9]#{v}$/ }

  if pair
    str[0...-1].to_f * (options[:base] ** pair.first)
  else
    str.to_f
  end
end

Instance Method Details

#round(val, ndigits) ⇒ Object



43
44
45
# File 'lib/si/module.rb', line 43

def round val, ndigits
  val.round ndigits
end

#si(options = {}) ⇒ Object



55
56
57
# File 'lib/si/module.rb', line 55

def si options = {}
  SI.convert(self, options)
end

#si_byte(length = 3) ⇒ Object



59
60
61
# File 'lib/si/module.rb', line 59

def si_byte length = 3
  SI.convert(self, :length => length, :base => 1024, :min_exp => 0) + 'B'
end