Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/procview/stats.rb

Constant Summary collapse

L =
1_000
S =
1_000 * L
M =
60 * S
H =
60 * M
D =
24 * H
Kilo =

Print a number with SI suffixes to the required precision

10**3
Mega =
10**6
Giga =
10**9
Tera =
10**12
Peta =
10**15
Exa =
10**18

Instance Method Summary collapse

Instance Method Details

#to_duration(max_digits = 3) ⇒ Object

Transform a number of seconds with SI suffixes



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/procview/stats.rb', line 8

def to_duration( max_digits=3 )
  usec = self.to_f.abs*1000000
  return "-" if usec == 0
  value, suffix = case usec
    when 0...L then [ usec, 'µS' ]
    when L...S then [ usec / L, 'mS' ]
    when S...M then [ usec / S, 'S' ]
    when M...H then [ usec / M, 'm' ]
    when H...D then [ usec / H, 'h' ]
    else            [ usec / D, 'd' ]
  end
  used_digits = case value
    when   0...10   then 1
    when  10...100  then 2
    when 100...1000 then 3
  end
  precision = max_digits - used_digits
  precision = 0 if precision < 0
  "#{self < 0 ? '-':''}%.#{precision}f#{suffix}" % value
end

#to_si(sig_digits = 3) ⇒ Object

Zetta = 10**21 Yotta = 10**24

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/procview/stats.rb', line 39

def to_si( sig_digits=3 )
  raise ArgumentError.new("sig_digits must be > 0") unless sig_digits > 0
  count = self.to_f.abs
  value, suffix = case count
    when 0...Kilo then [ count, '' ]
    when Kilo...Mega then [ count / Kilo, 'k' ]
    when Mega...Giga then [ count / Mega, 'M' ]
    when Giga...Tera then [ count / Giga, 'G' ]
    when Tera...Peta then [ count / Tera, 'T' ]
    when Peta...Exa then [ count / Peta, 'P' ]
    else            [ count / Exa, 'E' ]
  end
  used_digits = case value
    when   0...10   then 1
    when  10...100  then 2
    when 100...1001 then 3
  end
  precision = sig_digits - used_digits
  precision = 0 if precision < 0
  "#{self < 0 ? '-':''}%.#{precision}f#{suffix}" % value
end