将红宝石浮点数向上或向下舍入到最接近的 0.05

发布于 2024-08-03 09:28:26 字数 149 浏览 4 评论 0原文

我收到诸如“

2.36363636363636
4.567563
1.234566465448465
10.5857447736

如何让 Ruby 将这些数字向上(或向下)四舍五入到最接近的 0.05?”之类的数字?

I'm getting numbers like

2.36363636363636
4.567563
1.234566465448465
10.5857447736

How would I get Ruby to round these numbers up (or down) to the nearest 0.05?

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

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

发布评论

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

评论(9

⊕婉儿 2024-08-10 09:28:26
[2.36363636363636, 4.567563, 1.23456646544846, 10.5857447736].map do |x|
  (x*20).round / 20.0
end
#=> [2.35, 4.55, 1.25, 10.6]
[2.36363636363636, 4.567563, 1.23456646544846, 10.5857447736].map do |x|
  (x*20).round / 20.0
end
#=> [2.35, 4.55, 1.25, 10.6]
一百个冬季 2024-08-10 09:28:26

查看此链接,我认为这就是您所需要的。
Ruby 舍入

class Float
  def round_to(x)
    (self * 10**x).round.to_f / 10**x
  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
end

Check this link out, I think it's what you need.
Ruby rounding

class Float
  def round_to(x)
    (self * 10**x).round.to_f / 10**x
  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
end
独自←快乐 2024-08-10 09:28:26

一般来说,“舍入到最接近的x”的算法是:

round(x / precision)) * precision

有时乘以1 / precision会更好,因为它是一个整数(因此它的工作速度更快一些) ):

round(x * (1 / precision)) / (1 / precision)

在你的情况下,这将是:

round(x * (1 / 0.05)) / (1 / 0.05)

它将评估为:

round(x * 20) / 20;

不过,我不知道任何Python,所以语法可能不正确,但我相信你可以弄清楚。

In general the algorithm for “rounding to the nearest x” is:

round(x / precision)) * precision

Sometimes is better to multiply by 1 / precision because it is an integer (and thus it works a bit faster):

round(x * (1 / precision)) / (1 / precision)

In your case that would be:

round(x * (1 / 0.05)) / (1 / 0.05)

which would evaluate to:

round(x * 20) / 20;

I don’t know any Python, though, so the syntax might not be correct but I’m sure you can figure it out.

这样的小城市 2024-08-10 09:28:26

要四舍五入到最接近的 2 位:

(5.65235534).round(2)
#=> 5.65

To round to the nearest 2 places instead:

(5.65235534).round(2)
#=> 5.65
嗫嚅 2024-08-10 09:28:26

这是一个按任何给定步长值舍入的通用函数:

放置在 lib:

lib/rounding.rb
class Numeric
  # round a given number to the nearest step
  def round_by(increment)
    (self / increment).round * increment
  end
end

以及 spec:

require 'rounding'
describe 'nearest increment by 0.5' do
  {0=>0.0,0.5=>0.5,0.60=>0.5,0.75=>1.0, 1.0=>1.0, 1.25=>1.5, 1.5=>1.5}.each_pair do |val, rounded_val|
    it "#{val}.round_by(0.5) ==#{rounded_val}" do val.round_by(0.5).should == rounded_val end
  end
end

和用法:

require 'rounding'
2.36363636363636.round_by(0.05)

hth 中。

Here's a general function that rounds by any given step value:

place in lib:

lib/rounding.rb
class Numeric
  # round a given number to the nearest step
  def round_by(increment)
    (self / increment).round * increment
  end
end

and the spec:

require 'rounding'
describe 'nearest increment by 0.5' do
  {0=>0.0,0.5=>0.5,0.60=>0.5,0.75=>1.0, 1.0=>1.0, 1.25=>1.5, 1.5=>1.5}.each_pair do |val, rounded_val|
    it "#{val}.round_by(0.5) ==#{rounded_val}" do val.round_by(0.5).should == rounded_val end
  end
end

and usage:

require 'rounding'
2.36363636363636.round_by(0.05)

hth.

向日葵 2024-08-10 09:28:26

Ruby 2 现在有一个 round 函数:

# Ruby 2.3
(2.5).round
 3

# Ruby 2.4
(2.5).round
 2

ruby 2.4 中还有一些选项,例如::even:up:down< /代码>
例如;

(4.5).round(half: :up)
 5

Ruby 2 now has a round function:

# Ruby 2.3
(2.5).round
 3

# Ruby 2.4
(2.5).round
 2

There are also options in ruby 2.4 like: :even, :up and :down
e.g;

(4.5).round(half: :up)
 5
铜锣湾横着走 2024-08-10 09:28:26

可以使用 String 类的 % 方法对数字进行四舍五入。

例如,

"%.2f" % 5.555555555

将给出 "5.56" 作为结果(字符串)。

It’s possible to round numbers with String class’s % method.

For example

"%.2f" % 5.555555555

would give "5.56" as result (a string).

锦爱 2024-08-10 09:28:26

要获得不带小数的舍入结果,请使用 Float 的 .圆形

5.44.round
=> 5

5.54.round
=> 6

To get a rounding result without decimals, use Float's .round

5.44.round
=> 5

5.54.round
=> 6
归属感 2024-08-10 09:28:26

我知道这个问题很老了,但我喜欢与世界分享我的发明以帮助其他人:这是一种用步长舍入浮点数,将小数舍入到最接近给定数字的方法;它对于四舍五入产品价格很有用,例如:

def round_with_step(value, rounding)
  decimals = rounding.to_i
  rounded_value = value.round(decimals)

  step_number = (rounding - rounding.to_i) * 10
  if step_number != 0
    step = step_number * 10**(0-decimals)
    rounded_value = ((value / step).round * step)
  end

  return (decimals > 0 ? "%.2f" : "%g") % rounded_value
end

# For example, the value is 234.567
#
# | ROUNDING | RETURN | STEP
# | 1        | 234.60 | 0.1
# | -1       | 230    | 10
# | 1.5      | 234.50 | 5 * 0.1 = 0.5
# | -1.5     | 250    | 5 * 10  = 50
# | 1.3      | 234.60 | 3 * 0.1 = 0.3
# | -1.3     | 240    | 3 * 10  = 30

I know that the question is old, but I like to share my invention with the world to help others: this is a method for rounding float number with step, rounding decimal to closest given number; it's usefull for rounding product price for example:

def round_with_step(value, rounding)
  decimals = rounding.to_i
  rounded_value = value.round(decimals)

  step_number = (rounding - rounding.to_i) * 10
  if step_number != 0
    step = step_number * 10**(0-decimals)
    rounded_value = ((value / step).round * step)
  end

  return (decimals > 0 ? "%.2f" : "%g") % rounded_value
end

# For example, the value is 234.567
#
# | ROUNDING | RETURN | STEP
# | 1        | 234.60 | 0.1
# | -1       | 230    | 10
# | 1.5      | 234.50 | 5 * 0.1 = 0.5
# | -1.5     | 250    | 5 * 10  = 50
# | 1.3      | 234.60 | 3 * 0.1 = 0.3
# | -1.3     | 240    | 3 * 10  = 30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文