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

Instance Method Summary collapse

Instance Method Details

#to_duration(max_digits = 3) ⇒ Object



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

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