This is a simple gem (really simple), to handle money formats (USD, BRL and BTC. You can set your own format.).
Add this line to your application's Gemfile:
gem 'simple_magic_currency', git: "https://github.com/fabioaraujo121/simple_magic_currency", branch: "master"
And then execute:
$ bundle
- Make sure your money attribute type is Integer or Float or Decimal or BigDecimal or String
- Just use the method
to_currency
- Or use
to_currency(SimpleMagicCurrency::BRL)
when you haven't setted default or want something different from default
product = Product.new(price: 19.99)
product.price.to_currency # => R$ 19,99
product.price.to_currency(SimpleMagicCurrency::USD) # => US$ 19,99
product.price.to_currency(SimpleMagicCurrency::BTC, {unit: "ETH"}) # => 19.990000 ETH
- Create a file
/config/initializers/simple_magice_currency.rb
file - Add
SimpleMagicCurrency::DEFAULT = SimpleMagicCurrency::USD
, to use Dollar format, for example
- BRL
- USD
- BTC
- Any other using the option params
to_currency(nil, {delimiter: ',', separator: '.', unit: 'US$', precision: '2', position: 'before'})
Feel free to contribute!!! \o/
Bug reports and pull requests are welcome on GitHub at https://github.com/fabioaraujo121/simple_magic_currency. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the SimpleMagicCurrency project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.