Method: CrazyMoney#with_currency

Defined in:
lib/crazy_money.rb

#with_currency(iso_code) ⇒ Object

FIXME: needs polishing



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/crazy_money.rb', line 69

def with_currency iso_code
  currency = currency(iso_code) || raise(ArgumentError, "Unknown currency: #{iso_code.inspect}")

  left, right = to_s(decimal_places: currency.decimal_places).split(".")
  decimal_mark = right.nil? ? "" : currency.decimal_mark
  sign = left.slice!("-")

  left = left.reverse.scan(/.{1,3}/).map(&:reverse).reverse. # split every 3 digits right-to-left
    join(thousands_separator)

  formatted = [sign, left, decimal_mark, right].join

  if currency.symbol_first
    [currency.prefered_symbol, formatted]
  else
    [formatted, " ", currency.prefered_symbol]
  end.join
end