Skip to content

Commit

Permalink
add optional currency argument to Numeric#to_money
Browse files Browse the repository at this point in the history
  • Loading branch information
semmons99 committed Jul 9, 2010
1 parent b93de1a commit bad6f51
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/money/core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Numeric
# 100.37.to_money => #<Money @cents=10037>
# require 'bigdecimal'
# BigDecimal.new('100').to_money => #<Money @cents=10000>
def to_money
Money.new((self * 100).to_int)
def to_money(currency = Money.default_currency)
Money.new((self * 100).to_int, currency)
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/core_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
money.cents.should == 1234_00
money.currency.should == Money.default_currency
end

specify "Numeric#to_money accepts optional currency" do
1234.to_money('USD').should == Money.new(123400, 'USD')
1234.to_money('EUR').should == Money.new(123400, 'EUR')
end

specify "String#to_money works" do
"20.15".to_money.should == Money.new(20_15)
Expand Down

0 comments on commit bad6f51

Please sign in to comment.