Skip to content

Commit

Permalink
fix rounding error in Numeric#to_money
Browse files Browse the repository at this point in the history
  • Loading branch information
semmons99 committed Jul 23, 2010
1 parent 8f106b8 commit dfae9cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/money/core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class Numeric
# BigDecimal.new('100').to_money => #<Money @cents=10000>
def to_money(currency = Money.default_currency)
currency = Money::Currency.new(currency) unless currency.is_a?(Money::Currency)
Money.new((self * currency.subunit_to_unit).to_int, currency)
amt = self * currency.subunit_to_unit
amt = case amt.class.to_s
when 'BigDecimal'
amt.to_s('F')
else
amt.to_s
end
Money.new(amt.to_i, 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 "#issue/15" do
amount = 555.55.to_money
amount.should == Money.new(55555)
end

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

0 comments on commit dfae9cf

Please sign in to comment.