Docunext


Ruby Casting Strangeness

November 25th, 2009

This one has had me chasing my tail for the past hour... and I finally found a reproducible example:

$ irb1.8
irb(main):074:0> amt = "1298.34"
=> "1298.34"
irb(main):075:0> amt.to_f
=> 1298.34
irb(main):076:0> (amt.to_f * 100).to_i
=> 129833
irb(main):077:0> (amt.to_f * 100)
=> 129834.0
irb(main):078:0> amtf = (amt.to_f * 100)
=> 129834.0
irb(main):079:0> amtf.to_i
=> 129833
irb(main):080:0> amt2 = 129834.0
=> 129834.0
irb(main):081:0> amt2
=> 129834.0
irb(main):082:0> amt2.to_i
=> 129834

In general, I realize that Float arithmetic is different than what I'd expect it to be, but that's a little ridiculous.

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022