Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/stock_quote_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#number_with_commasObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/stock_quote_cli/cli.rb', line 144

def number_with_commas
  split_on_dot = to_s.split("\.")
  whole = split_on_dot[0]
  decimal = split_on_dot[1] || ""
  char_array = whole.reverse.split(//)
  whole_with_commas = char_array.each_with_index.map do |char, i|
    if char_array.size > 3 && i % 3 == 0 && i > 0
      "#{char},"
    else
      char
    end
  end.reverse.join("")
  if decimal.size == 1
    decimal = "#{decimal}0"
  end
  unless decimal == "" || decimal == "00"
    "#{whole_with_commas}.#{decimal}"
  else
    "#{whole_with_commas}"
  end
end