红宝石1.9.2的加法错误 [英] Addition error with ruby-1.9.2

查看:120
本文介绍了红宝石1.9.2的加法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加0.1+0.2时,我会得到0.30000000000000004,但是当我在ruby 1.8.7中添加相同的数字时,我会得到正确的答案0.3.我通过四舍五入得到0.3,但我只想通过添加0.10.2

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

推荐答案

您需要使用十进制才能使其正常工作.

You need bigdecimal for this to make work.


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

请参见以下链接:

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

这篇关于红宝石1.9.2的加法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆