gpt4 book ai didi

ruby-on-rails - RoR 中的简单舍入在一个非常奇怪的实例中不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 08:51:16 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Arithmetic in ruby

status_update 模型接受 2 个值,对它们进行一些简单的计算,并从那里分配 'current_lbm' 和 'current_bf_pct'。

current_lbm 和 current_bf_pct 如果我不限制它们将产生大量小数,所以我使用 .round(2) 方法将值限制为小数点后两位。

这是执行此操作的方法。

after_initialize :default_values
.
.
def default_values
if self.current_bf_pct >= 0.5
self.current_bf_pct /= 100
if self.current_bf_pct <= 0.04
self.current_fb_pct *= 100
end
end
self.current_fat_weight = self.current_weight * self.current_bf_pct
self.current_lbm = self.current_weight - self.current_fat_weight
self.current_fat_weight = self.current_fat_weight.round(2)
self.current_lbm = self.current_lbm.round(2)
end

结果如下:

09/13/2012 186.0 4.5 177.63 8.37
09/13/2012 187.0 5.5 176.72 10.29
09/13/2012 188.0 6.5 175.78 12.22
09/13/2012 189.0 7.5 174.83 14.18
09/13/2012 190.0 8.5 173.85 16.15
09/13/2012 191.0 9.5 172.86 18.15
09/13/2012 192.0 10.5 171.84 20.16
09/13/2012 193.0 11.5 170.81 22.2
09/13/2012 194.0 12.5 169.75 24.25
09/13/2012 195.0 13.5 168.68 26.33
09/13/2012 196.0 14.499999999999998 167.58 28.42
09/13/2012 197.0 15.5 166.47 30.54
09/13/2012 198.0 16.5 165.33 32.67
09/13/2012 199.0 17.5 164.18 34.82
09/13/2012 200.0 18.5 163.0 37.0

我的问题是... 14.499999999999998 到底是怎么回事??

这些是 View 的结果。 current_bf_pct 行是日期中的第 3 行。它们不是小数,因为我在 View 中将它们乘以 100。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com