# Float math?

**URL:** <https://www.ruby-forum.com/t/float-math/120698>\
**Category:** Ruby\
**Created:** [November 19, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/float-math/120698 "2007-11-19T22:05:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [November 19, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/float-math/120698/1 "2007-11-19T22:05:35Z")

</div>

hey all,

I’ve got a delimited file with lines like so…

1234^2007-11-17^4321^Peter Pan^123 test  
st^^portland^ir^97101^US^^^Statement 34567^87.92^Statement for  
2007-11-17

quick script to total col 13

require ‘csv’  
file = “myFile.txt”  
total = 0.0  
CSV.open(file, “r”, ‘^’) do |row|  
print "#{total} + "  
total += row[13].to\_f  
puts “#{row[13]} = #{total}”  
end

output:  
…  
2415.06 + 20.99 = 2436.05  
…  
…  
6704.58 + 87.92 = 6792.5  
6792.5 + 75.0 = 6867.5  
6867.5 + 15.98 = 6883.48  
\<= so far so good!  
6883.48 + 15.99 = 6899.46999999999 \<= uhh oh…  
what happened?  
6899.46999999999 + 50.0 = 6949.46999999999  
6949.46999999999 + 32.0 = 6981.46999999999  
…  
…

Why do all the extra decimal places all of the sudden come into play?

Thanks for your time!  
Tim

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [November 19, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/float-math/120698/2 "2007-11-19T22:16:42Z")

</div>

On Nov 19, 2007 10:05 PM, Tim McIntyre [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘csv’  
> 2415.06 + 20.99 = 2436.05  
> …  
> …
> 
> Why do all the extra decimal places all of the sudden come into play?
> 
> Thanks for your time!  
> Tim

Search the archive for ‘float’, or possibly ‘float precision’. It’s a  
FAQ.
