Can't output currency in ruby with money-gem

If I run this code:

require "money"

price1 = Money.new(100)
price2 = Money.new(2000)

total = price1 + price2

puts total

I get a I18n::InvalidLocale error:

[path to ruby]/gems/i18n-0.7.0/lib/i18n.rb:284:in `enforce_available_locales!': :en is not a valid locale (I18n::InvalidLocale)

What can I do to avoid this problem?

  • sharing ruby code within an organisation
  • Can't Run Rails Server, Could Not Find Gem 'rails(4.2.5.1) x86-mingw32'
  • How do I access the Rack environment from within Rails?
  • How Do I Access This Variable?
  • Ruby Array find_first object?
  • How to modify a class inside a module inside a ruby gem
  • Ruby encrypting passwords to be stored in config file
  • Finding the IP address of a domain
  • 3 Solutions collect form web for “Can't output currency in ruby with money-gem”

    The money gem uses I18n. You can either add a valid locale or disable I18n:

    require 'money'
    
    Money.new(100).format  #=> I18n::InvalidLocale: :en is not a valid locale
    
    Money.use_i18n = false
    
    Money.new(100).format  #=> "$1.00"
    

    Try this:

    require "money"
    
    price1 = Money.new(100)
    price2 = Money.new(2000)
    
    total = price1 + price2
    
    puts "#{total.fractional} #{total.currency}"
    
    require "money"
    
    price1 = Money.new(100)
    price2 = Money.new(2000)
    
    total = price1 + price2
    
    puts "#{total} #{total.currency}"
    
    Ruby is the best programming language in the world - Ruby on Rails.