gpt4 book ai didi

Ruby四舍五入向上或向下 float 到特定的十进制有效数字

转载 作者:行者123 更新时间:2023-12-04 03:00:29 25 4
gpt4 key购买 nike

我正在使用 Ruby 2.3.1,这是我想要做的:

1.33333333.ceil(2) -> 1.34
1.33333333.floor(3) -> 1.333
Float#round方法允许我舍入,但我需要能够指定是向上还是向下舍入,类似于 #ceil#floor方法,但带有一个参数来指定要保留的小数位数。

最佳答案

Ruby 2.4+ , Float#float & Float#ceil方法采用 ndigits争论:

1.33333333.ceil(2) -> 1.34
1.33333333.floor(3) -> 1.333

然而 ,使用这些 STD lib 方法检查此行为:
# In Ruby 2.4.2:
0.07.ceil(2) -> 0.08
1.1.ceil(2) -> 1.11

在我的书中不好。

对于较旧的 Ruby 版本,或者如果您想获得比 STB 库提供的更好的结果,则需要编写自己的方法。有一些不同的博客文章,稍后我将解释为什么它们不是始终正确的,但这里有一些方法每次都应该正确工作:
require 'bigdecimal'

class Float
def ceil2(exp = 0)
BigDecimal(self.to_s).ceil(exp).to_f
end

def floor2(exp = 0)
BigDecimal(self.to_s).floor(exp).to_f
end
end

现在了解更多原因 以下不正确 :
def ceil_to(x)
(self * 10**x).ceil.to_f / 10**x
end

def floor_to(x)
(self * 10**x).floor.to_f / 10**x
end

# These methods also produce bad results for the examples shown above
0.07.ceil(2) -> 0.08
1.1.ceil(2) -> 1.11

我不会详细介绍正在发生的事情(你可以找到
herehere ),但浮点运算可能很困惑,并且确实会发生舍入错误。

关于Ruby四舍五入向上或向下 float 到特定的十进制有效数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49656493/

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