Skip to content

Commit

Permalink
don't use to_f to parse strings; too lossy!
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Dec 21, 2010
1 parent e951d57 commit ee18bbc
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/models/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ class Statement < ActiveRecord::Base
validates_presence_of :occurred_on, :ending_balance

def ending_balance=(amount)
if amount.is_a?(Float) || amount =~ /[.,]/
if amount.is_a?(Float)
amount = (amount.to_s.tr(",", "").to_f * 100).round
elsif amount =~ /[.,]/
dollars, cents = amount.split(".")
dollars = dollars.tr(",", "").to_i

cents = cents[0,2]
cents << "0" while cents.length < 2
cents = cents.to_i

amount = dollars * 100 + cents
end

super(amount)
Expand Down

0 comments on commit ee18bbc

Please sign in to comment.