Ruby-on-rails 一种方法来舍入 Floats down
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7090537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A way to round Floats down
提问by Michael Koper
Float round rounds it up or down. I always need it to round down.
浮动轮将其向上或向下舍入。我总是需要它来四舍五入。
I have the solution but i dont really like it... Maybe there is a better way.
我有解决方案,但我真的不喜欢它......也许有更好的方法。
This is what i want:
这就是我要的:
1.9999.round_down(2)
#=> 1.99
1.9901.round_down(2)
#=> 1
I came up with this solution but i would like to know if there is a better solution(I dont like that i convert the float twice). Is there already a method for this? Because I found it pretty strange that I couldnt find it.
我想出了这个解决方案,但我想知道是否有更好的解决方案(我不喜欢我将浮点数转换两次)。是否已经有一种方法?因为我觉得很奇怪,我找不到它。
class Float
def round_down(n=0)
((self * 10**n).to_i).to_f/10**n
end
end
Thanks.
谢谢。
采纳答案by geronime
Based on answer from @kimmmo this should be a little more efficient:
根据@kimmmo 的回答,这应该更有效率:
class Float
def round_down n=0
s = self.to_s
l = s.index('.') + 1 + n
s.length <= l ? self : s[0,l].to_f
end
end
1.9991.round_down(3)
=> 1.999
1.9991.round_down(2)
=> 1.99
1.9991.round_down(0)
=> 1.0
1.9991.round_down(5)
=> 1.9991
or based on answer from @steenslag, probably yet more efficient as there is no string conversion:
或基于@steenslag 的回答,可能更高效,因为没有字符串转换:
class Float
def round_down n=0
n < 1 ? self.to_i.to_f : (self - 0.5 / 10**n).round(n)
end
end
回答by fl00r
1.9999.to_i
#=> 1
1.9999.floor
#=> 1
answered 1 sec ago fl00r
1 秒前回答 fl00r
"%.2f" % 1.93213
#=> 1.93
@kimmmo is right.
@kimmmo 是对的。
class Float
def round_down(n=0)
self.to_s[/\d+\.\d{#{n}}/].to_f
end
end
回答by Kimmo Lehto
Looks like you just want to strip decimals after n
看起来你只想在n之后去掉小数
class Float
def round_down(n=0)
int,dec=self.to_s.split('.')
"#{int}.#{dec[0...n]}".to_f
end
end
1.9991.round_down(3)
=> 1.999
1.9991.round_down(2)
=> 1.99
1.9991.round_down(0)
=> 1.0
1.9991.round_down(10)
=> 1.9991
(Edit: slightly more efficient version without the regexp)
(编辑:没有正则表达式的稍微更高效的版本)
回答by Tejo
回答by steenslag
In Ruby 1.9:
在 Ruby 1.9 中:
class Float
def floor_with_prec(prec = 0)
(self - 0.5).round(prec)
end
end
回答by Dongzhen Piao
class Float
def rownd_down(digits = 1)
("%.#{digits+1}f" % self)[0..-2].to_f
end
end
> 1.9991.rownd_down(3)
=> 1.999
> 1.9991.rownd_down(2)
=> 1.99
> 1.9991.rownd_down(10)
> 1.9991
回答by Bob Lu
Found a bug for the answers that try to calculate float in round_down method.
发现尝试在 round_down 方法中计算浮点数的答案的错误。
> 8.7.round_down(1)
=> 8.7
> 8.7.round_down(2)
=> 8.69
you can use bigdecimal, integer or maybe string to do all the math but float.
您可以使用 bigdecimal、integer 或 string 来完成所有数学运算,但浮点数除外。
> 8.7 - 0.005
=> 8.694999999999999
Here is my solution:
这是我的解决方案:
require 'bigdecimal'
class Float
def floor2(n = 0)
BigDecimal.new(self.to_s).floor(n).to_f
end
end
> 8.7.floor2(1)
=> 8.7
> 8.7.floor2(2)
=> 8.7
> 1.9991.floor(3)
=> 1.999
> 1.9991.floor(2)
=> 1.99
> 1.9991.floor(1)
=> 1.9
回答by EJAg
class Float
def round_down(n)
num = self.round(n)
num > self ? (num - 0.1**n) : num
end
end
56.0.round_down(-1) = 50. Works with negative numbers as well, if you agree that rounding down makes a number smaller: -56.09.round_down(1) = -56.1.
56.0.round_down(-1) = 50. 与负数的作品为好,如果你同意,四舍五入,使一些较小的:-56.09.round_down(1) = -56.1。
回答by Eric
Found this article helpful: https://richonrails.com/articles/rounding-numbers-in-ruby
发现这篇文章很有帮助:https: //richonrails.com/articles/rounding-numbers-in-ruby
Here are the round up and down methods:
以下是向上和向下舍入的方法:
class Float
def round_down(exp = 0)
multiplier = 10 ** exp
((self * multiplier).floor).to_f/multiplier.to_f
end
def round_up(exp = 0)
multiplier = 10 ** exp
((self * multiplier).ceil).to_f/multiplier.to_f
end
end
回答by Tejas
This worked for me.
这对我有用。
> (1.999).to_i.to_f
For rounding up you could just use
对于四舍五入,您可以使用
> (1.999+1).to_i.to_f

