Module: InvoicePDF::Helpers
- Included in:
- Generators::Standard
- Defined in:
- lib/invoice/helpers.rb
Overview
Helpers to be used in InvoicePDF::Generators
Instance Method Summary collapse
-
#money_maker(number, options = {}) ⇒ Object
Similar to Rails’ number_to_currency, but we don’t want to rely on Rails.
Instance Method Details
#money_maker(number, options = {}) ⇒ Object
Similar to Rails’ number_to_currency, but we don’t want to rely on Rails. Found the majority of this code online, but lost the URL. If this is yours, let me know and I’ll give credit.
Formats the number
into a currency format. You can customize the format in the options
hash.
Options
-
:currency_symbol
- Currency symbol. (default is ‘$’) -
:delimiter
- Thousands separator. (default is ‘,’) -
:decimal_symbol
- Separates integer and fractional amounts (think dollars and cents). (default is ‘.’) -
:currency_before
- Boolean to set placement of:currency_symbol
. true for before number, false for after number. (default is true) -
:after_text
- Useful for displaying the currency after the number. (default is ‘ USD’)
Example
money_maker(1000) # => $1,000.00 USD
money_maker(1000, :delimiter => '.', :decimal_symbol => ',', :after_text => '') # => $1.000,00
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/invoice/helpers.rb', line 21 def money_maker( number, = {} ) = { :currency_symbol => '$', :delimiter => ',', :decimal_symbol => '.', :currency_before => true, :after_text => ' USD' }.merge() # split integer and fractional parts int, frac = ("%.2f" % number).split('.') # insert delimiters int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{[:delimiter]}") return [:currency_symbol] + int + [:decimal_symbol] + frac + [:after_text] if [:currency_before] int + [:decimal_symbol] + frac + [:currency_symbol] + [:after_text] end |