class ShortScale::Formatter

Public Class Methods

new(config = {}) click to toggle source
# File lib/short_scale/formatter.rb, line 3
def initialize(config = {})
  @base = config[:base]
  @tier = config[:tier]
end

Public Instance Methods

format(number) click to toggle source
# File lib/short_scale/formatter.rb, line 8
def format(number)
  value(number) + @tier[:unit]
end

Private Instance Methods

value(number) click to toggle source
# File lib/short_scale/formatter.rb, line 13
def value(number)
  # This truncates n to a single decimal and remove .0 when necessary
  n = number / @base ** (@tier[:magnitude] - 1)

  "#{n.to_i / @base.to_f}".sub('.0', '')
end