Class: Numeric

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

Instance Method Summary collapse

Instance Method Details

#get_number_decimals_msObject

return the number of decimal points that this number would best render in



304
305
306
307
308
309
310
311
312
313
314
# File 'lib/new_relic/stats.rb', line 304

def get_number_decimals_ms
  base = 0.010
  decimal = 0
  
  while decimal <= 6 && self < base do
    base /= 10.0
    decimal += 1
  end
  
  decimal
end

#round_to(decimal_places) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/new_relic/stats.rb', line 336

def round_to(decimal_places)
  x = self
  decimal_places.times do
    x = x * 10
  end
  x = x.round
  decimal_places.times do
    x = x.to_f / 10
  end
  x
end

#round_to_1Object



348
349
350
# File 'lib/new_relic/stats.rb', line 348

def round_to_1
  round_to(1)
end

#round_to_2Object



352
353
354
# File 'lib/new_relic/stats.rb', line 352

def round_to_2
  round_to(2)
end

#round_to_3Object



356
357
358
# File 'lib/new_relic/stats.rb', line 356

def round_to_3
  round_to(3)
end

#to_minutes(decimal_places = 0) ⇒ Object



326
327
328
# File 'lib/new_relic/stats.rb', line 326

def to_minutes(decimal_places = 0)
  (self / 60).round_to(decimal_places)
end

#to_ms(decimal_places = 0) ⇒ Object

utlity method that converts floating point time values in seconds to integers in milliseconds, to improve readability in ui



298
299
300
# File 'lib/new_relic/stats.rb', line 298

def to_ms(decimal_places = 0)
  (self * 1000).round_to(decimal_places)
end

#to_ns(decimal_places = 0) ⇒ Object



322
323
324
# File 'lib/new_relic/stats.rb', line 322

def to_ns(decimal_places = 0)
  (self * 1000000).round_to(decimal_places)
end

#to_percentage(decimal_places = 2) ⇒ Object

utility method that converts floating point percentage values to integers as a percentage, to improve readability in ui



332
333
334
# File 'lib/new_relic/stats.rb', line 332

def to_percentage(decimal_places = 2)
  (self * 100).round_to(decimal_places)
end

#to_smart_msObject

auto-adjust the precision based on the value



317
318
319
# File 'lib/new_relic/stats.rb', line 317

def to_smart_ms
  to_ms get_number_decimals_ms
end

#with_delimiter(delimiter = ",", separator = ".") ⇒ Object

copied from rails



286
287
288
289
290
291
292
293
294
# File 'lib/new_relic/stats.rb', line 286

def with_delimiter(delimiter=",", separator=".")
  begin
    parts = self.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  rescue
    self
  end
end