Skip to content

Instantly share code, notes, and snippets.

@marcomontes
Last active August 29, 2015 14:22
Show Gist options
  • Save marcomontes/4d0403eef86954aa4b1e to your computer and use it in GitHub Desktop.
Save marcomontes/4d0403eef86954aa4b1e to your computer and use it in GitHub Desktop.
Currency Format - Convierte un número a formato de moneda, con símbolo y delimitadores
def currency_format(number, options={})
options = {:currency_symbol => "$", :delimiter => ".", :decimal_symbol => ",", :currency_before => true, :no_decimal => true}.merge(options)
# split integer and fractional parts
int, frac = ("%.2f" % number).split('.')
# insert the delimiters
int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
if options[:no_decimal]
frac_part = ""
else
frac_part = options[:decimal_symbol] + frac
end
if options[:currency_before]
options[:currency_symbol] + int + frac_part
else
int + frac_part + options[:currency_symbol]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment