Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Addition error with ruby-1.9.2 [duplicate]

When I add 0.1+0.2 I am getting 0.30000000000000004 but when I add the same number in ruby 1.8.7 I am getting the correct answer 0.3. I get 0.3 by rounding but I just want to get 0.3 on ruby 1.9.2 by adding 0.1 and 0.2

like image 601
Sadiksha Gautam Avatar asked Mar 24 '11 05:03

Sadiksha Gautam


1 Answers

You need bigdecimal for this to make work.

(BigDecimal('0.1') + BigDecimal("0.2")).to_f

See below link:

http://redmine.ruby-lang.org/issues/4394

like image 80
ashisrai_ Avatar answered Oct 23 '22 04:10

ashisrai_