RubyMoney - Money

repository·main·Indexed 25 days ago

https://github.com/rubymoney/money

A Ruby library for handling monetary values and currency conversions. It uses integer-based cent representation to prevent floating-point errors and provides a robust system for managing currencies, exchange rates, and localization via the i18n gem. Key features include currency exchange through bank objects, custom currency registration, and configurable precision and rounding.

Tokens
6.8K
Snippets
25
Records
67
Agent score
83%

What's inside money

  1. Localize money formatting with I18n

    main

    To use the I18n gem for localized formatting (thousands separators, decimal marks, and symbol placement), set Money.locale_backend = :i18n. This relies on your translation files (e.g., en.yml) having the correct number.currency.format structure.

    # Enable I18n localization
    Money.locale_backend = :i18n
    
    I18n.locale = :en
    Money.from_cents(10_000_00, 'USD').format # => $10,000.00
    
    I18n.locale = :es
    Money.from_cents(10_000_00, 'USD').format # => $10.000,00
  2. Initialize Money with locales

    main

    To use Money with specific locales (e.g., using the i18n gem for localization), configure the locale backend and available locales during your application setup.

    require 'money'
    
    # explicitly define locales
    I18n.config.available_locales = :en
    Money.locale_backend = :i18n
  3. Configure exchange rate stores

    main
    The default bank uses an in-memory store. You can replace it with a custom store (e.g., database, Redis, or an API scraper) by assigning a new Money::Bank::VariableExchange instance to Money.default_bank.
  4. Configure Default Currency in Money 7.0

    main

    In Money 7.0, Money.default_currency changed from "USD" to nil. Initializing a Money object without a currency will now raise Currency::NoCurrency.

    To restore the previous behavior or set a specific default, use an initializer:

    # config/initializers/money.rb
    Money.setup_defaults
    Money.default_currency = Money::Currency.new("USD")

    Alternatively, always specify the currency explicitly when creating objects:

    Money.new(1_00, "USD")
  5. Handle BigDecimal return values from Money#dollars and Money#amount

    main

    In Money 6.0.0 and later, the Money#dollars and Money#amount methods return instances of BigDecimal instead of Float. This change was made to prevent precision errors associated with floating-point arithmetic.

    To maintain compatibility, you should:

    1. Recommended: Update your application logic to handle BigDecimal return values.
    2. Fallback: If your application strictly requires a Float, call .to_f on the result of #dollars or #amount (e.g., money.amount.to_f).
  6. Upgrade to Money v7.0 or v6.0

    main

    When performing major version upgrades, refer to the specific migration guides for breaking changes and step-by-step instructions:

    • Upgrading to 7.0: For migrating from 6.x to 7.0.
    • Upgrading to 6.0: For upgrading to version 6.0.
  7. Perform currency exchange

    main

    Exchanging money is performed through an exchange bank object. By default, you must manually specify exchange rates using Money.add_rate. Once rates are defined, you can use exchange_to to convert a Money object to a different currency.

    Money.add_rate("USD", "CAD", 1.24515)
    Money.add_rate("CAD", "USD", 0.803115)
    
    Money.us_dollar(100).exchange_to("CAD")  # => Money.from_cents(124, "CAD")
    Money.ca_dollar(100).exchange_to("USD")  # => Money.from_cents(80, "USD")
  8. Upgrade to Money 7.0 Requirements

    main

    To upgrade to Money 7.0, ensure your environment meets the following requirements:

    • Ruby Version: Upgrade to Ruby 3.1 or later.
    • i18n Version: Ensure you are using i18n ~> 1.9 in your Gemfile.

    Testing your upgrade

    1. Update your Gemfile to gem 'money', '~> 7.0'.
    2. Run bundle update money.
    3. Run your test suite (e.g., bundle exec rake test).
    4. Check for deprecation warnings and review formatting/mathematical operation outputs.
    # .ruby-version
    3.1.0
    # Gemfile
    gem 'i18n', '~> 1.9'
    bundle update money
  9. Configure Rounding Mode in Money 7.0

    main

    The default rounding mode changed from BigDecimal::ROUND_HALF_EVEN (banker's rounding) to BigDecimal::ROUND_HALF_UP (standard rounding).

    To maintain the old behavior (6.x), set the rounding mode explicitly in your initializer:

    # config/initializers/money.rb
    Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
  10. Configure Locale Backend for Formatting

    main

    The default locale backend changed from :legacy to :currency. The methods Money.use_i18n and Money.use_i18n= have been removed.

    To use i18n-based formatting (the old default behavior), set the backend in your initializer:

    # config/initializers/money.rb
    Money.locale_backend = :i18n

    To use the new default (currency-based formatting):

    # config/initializers/money.rb
    Money.locale_backend = :currency