Module: Brcobranca::Currency::Number
- Defined in:
- lib/brcobranca/currency.rb
Overview
Instance Method Summary collapse
- #to_currency(options = {}) ⇒ Object
- #with_delimiter(delimiter = ",", separator = ".") ⇒ Object
- #with_precision(precision = 3) ⇒ Object
Instance Method Details
#to_currency(options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/brcobranca/currency.rb', line 22 def to_currency( = {}) number = self default = Brcobranca::Currency::DEFAULT = default.merge() precision = [:precision] || default[:precision] unit = [:unit] || default[:unit] position = [:position] || default[:position] separator = precision > 0 ? [:separator] || default[:separator] : "" delimiter = [:delimiter] || default[:delimiter] begin parts = number.with_precision(precision).split('.') number = parts[0].to_i.with_delimiter(delimiter) + separator + parts[1].to_s position == "before" ? unit + number : number + unit rescue number end end |
#with_delimiter(delimiter = ",", separator = ".") ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/brcobranca/currency.rb', line 41 def with_delimiter(delimiter=",", separator=".") number = self begin parts = number.to_s.split(separator) parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") parts.join separator rescue self end end |
#with_precision(precision = 3) ⇒ Object
52 53 54 55 |
# File 'lib/brcobranca/currency.rb', line 52 def with_precision(precision=3) number = self "%01.#{precision}f" % number end |