Ruby 中的舍入浮点数

发布于 2024-08-18 01:08:18 字数 183 浏览 3 评论 0原文

我在四舍五入时遇到问题。我有一个浮点数,我想将其四舍五入到小数点后百分之几。但是,我只能使用 .round ,它基本上将其转换为 int,这意味着 2.34.round # => 2. 有没有一种简单的效果方法可以做类似 2.3465 # => 的事情2.35

I'm having problems rounding. I have a float, which I want to round to the hundredth of a decimal. However, I can only use .round which basically turns it into an int, meaning 2.34.round # => 2. Is there a simple effect way to do something like 2.3465 # => 2.35

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

儭儭莪哋寶赑 2024-08-25 01:08:18

将参数传递给 round,其中包含要舍入的小数位数

>> 2.3465.round
=> 2
>> 2.3465.round(2)
=> 2.35
>> 2.3465.round(3)
=> 2.347

Pass an argument to round containing the number of decimal places to round to

>> 2.3465.round
=> 2
>> 2.3465.round(2)
=> 2.35
>> 2.3465.round(3)
=> 2.347
邮友 2024-08-25 01:08:18

显示时,可以使用(例如)

>> '%.2f' % 2.3465
=> "2.35"

如果要四舍五入存储,可以使用

>> (2.3465*100).round / 100.0
=> 2.35

When displaying, you can use (for example)

>> '%.2f' % 2.3465
=> "2.35"

If you want to store it rounded, you can use

>> (2.3465*100).round / 100.0
=> 2.35
握住你手 2024-08-25 01:08:18

您可以使用它来四舍五入到精度..

//to_f is for float

salary= 2921.9121
puts salary.to_f.round(2) // to 2 decimal place                   

puts salary.to_f.round() // to 3 decimal place          

you can use this for rounding to a precison..

//to_f is for float

salary= 2921.9121
puts salary.to_f.round(2) // to 2 decimal place                   

puts salary.to_f.round() // to 3 decimal place          
独木成林 2024-08-25 01:08:18

你可以在 Float 类中添加一个方法,我从 stackoverflow 了解到这一点:

class Float
    def precision(p)
        # Make sure the precision level is actually an integer and > 0
        raise ArgumentError, "#{p} is an invalid precision level. Valid ranges are integers > 0." unless p.class == Fixnum or p < 0
        # Special case for 0 precision so it returns a Fixnum and thus doesn't have a trailing .0
        return self.round if p == 0
        # Standard case  
        return (self * 10**p).round.to_f / 10**p
    end
end

You can add a method in Float Class, I learnt this from stackoverflow:

class Float
    def precision(p)
        # Make sure the precision level is actually an integer and > 0
        raise ArgumentError, "#{p} is an invalid precision level. Valid ranges are integers > 0." unless p.class == Fixnum or p < 0
        # Special case for 0 precision so it returns a Fixnum and thus doesn't have a trailing .0
        return self.round if p == 0
        # Standard case  
        return (self * 10**p).round.to_f / 10**p
    end
end
且行且努力 2024-08-25 01:08:18

您还可以提供一个负数作为 round 方法的参数,以四舍五入到最接近的 10、100 等倍数。

# Round to the nearest multiple of 10. 
12.3453.round(-1)       # Output: 10

# Round to the nearest multiple of 100. 
124.3453.round(-2)      # Output: 100

You can also provide a negative number as an argument to the round method to round to the nearest multiple of 10, 100 and so on.

# Round to the nearest multiple of 10. 
12.3453.round(-1)       # Output: 10

# Round to the nearest multiple of 100. 
124.3453.round(-2)      # Output: 100
差↓一点笑了 2024-08-25 01:08:18

(2.3465*100).round()/100.0 怎么样?

what about (2.3465*100).round()/100.0?

独﹏钓一江月 2024-08-25 01:08:18
def rounding(float,precision)
    return ((float * 10**precision).round.to_f) / (10**precision)
end
def rounding(float,precision)
    return ((float * 10**precision).round.to_f) / (10**precision)
end
半夏半凉 2024-08-25 01:08:18

如果您只需要显示它,我会使用 number_with_ precision 帮助者。
如果您在其他地方需要它,我会使用 Steve Weet 指出的 round 方法

If you just need to display it, I would use the number_with_precision helper.
If you need it somewhere else I would use, as Steve Weet pointed, the round method

困倦 2024-08-25 01:08:18

对于 ruby​​ 1.8.7,您可以将以下内容添加到代码中:

class Float
    alias oldround:round
    def round(precision = nil)
        if precision.nil?
            return self
        else
            return ((self * 10**precision).oldround.to_f) / (10**precision)
        end 
    end 
end

For ruby 1.8.7 you could add the following to your code:

class Float
    alias oldround:round
    def round(precision = nil)
        if precision.nil?
            return self
        else
            return ((self * 10**precision).oldround.to_f) / (10**precision)
        end 
    end 
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文