如何将整数四舍五入到<最近的大数>在鲁比?

发布于 2024-09-12 08:18:14 字数 86 浏览 6 评论 0原文

假设我有以下任何号码:

230957 或 83487或 第4785

章 300000 或 90000 或 分别是5000?

Say I have any of the following numbers:

230957 or
83487 or
4785

What is a way in Ruby I could return them as
300000 or
90000 or
5000, respectively?

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

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

发布评论

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

评论(8

绳情 2024-09-19 08:18:14
def round_up(number)
  divisor = 10**Math.log10(number).floor
  i = number / divisor
  remainder = number % divisor
  if remainder == 0
    i * divisor
  else
    (i + 1) * divisor
  end
end

用你的例子:

irb(main):022:0> round_up(4785)
=> 5000    
irb(main):023:0> round_up(83487)
=> 90000
irb(main):024:0> round_up(230957)
=> 300000
def round_up(number)
  divisor = 10**Math.log10(number).floor
  i = number / divisor
  remainder = number % divisor
  if remainder == 0
    i * divisor
  else
    (i + 1) * divisor
  end
end

With your examples:

irb(main):022:0> round_up(4785)
=> 5000    
irb(main):023:0> round_up(83487)
=> 90000
irb(main):024:0> round_up(230957)
=> 300000
又爬满兰若 2024-09-19 08:18:14
def round_to_significant_digit(i, significant_digits = 1)
  exp = Math.log10(i).floor - (significant_digits - 1)
  (i / 10.0 ** exp).round * 10 ** exp
end

 >> [230957, 83487, 4785].collect{|i|round_to_significant_digit(i)}
 => [200000, 80000, 5000]

并获得额外学分:

 >>  [230957, 83487, 4785].collect{|i|round_to_significant_digit(i, 2)}
 => [230000, 83000, 4800]
 >>  [230957, 83487, 4785].collect{|i|round_to_significant_digit(i, 3)}
 => [231000, 83500, 4790]
def round_to_significant_digit(i, significant_digits = 1)
  exp = Math.log10(i).floor - (significant_digits - 1)
  (i / 10.0 ** exp).round * 10 ** exp
end

 >> [230957, 83487, 4785].collect{|i|round_to_significant_digit(i)}
 => [200000, 80000, 5000]

And for extra credit:

 >>  [230957, 83487, 4785].collect{|i|round_to_significant_digit(i, 2)}
 => [230000, 83000, 4800]
 >>  [230957, 83487, 4785].collect{|i|round_to_significant_digit(i, 3)}
 => [231000, 83500, 4790]
冷…雨湿花 2024-09-19 08:18:14

Math.round 接受负数。如果您只想查找最接近的 10 个,则可以执行 (my_num).round(-1)

唯一的缺点是这里无法合并 ceil,因此它并不总是向上舍入 - 4.round(-1) 将返回 0。

Math.round accepts negative numbers. If you are only looking for the nearest 10, you can do (my_num).round(-1).

The only drawback being that there's no way to incorporate ceil here, so it doesn't always round up -- 4.round(-1) will return 0.

红墙和绿瓦 2024-09-19 08:18:14

在 Rails 中,您可能还喜欢“number_to_ human”帮助器,它会自动选择一个合适的维度进行舍入。

http://api.rubyonrails.org/classes/ActionView /Helpers/NumberHelper.html#method-i-number_to_ human

In Rails, you may also like the "number_to_human" helper, which automatically chooses a good dimension to round to.

http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_human

够运 2024-09-19 08:18:14

我实际上没有在 Ruby 中完成任何编码,但是如果您首先将其推到您想要的数字,您就可以使用标准舍入函数来完成此操作。

示例:

230957 / 100000(the resolution you want) = 2.30957

舍入 2.30957 = 2,或舍入到上限/舍入值 + 0.5 以使其达到较高值而不是较低值。

2 or 3 * 100000(the resolution you want) = 200000 or 300000 respectively.

希望这有帮助!

I haven't actually done any coding in Ruby, but you would be able to do that with a standard rounding function if you pushed it over to the digit you wanted first.

Example:

230957 / 100000(the resolution you want) = 2.30957

Round 2.30957 = 2, or Round to Ceiling/Round value + 0.5 to get it to go to the upper value rather than the lower.

2 or 3 * 100000(the resolution you want) = 200000 or 300000 respectively.

Hope this helps!

孤者何惧 2024-09-19 08:18:14

它看起来有点难看,但作为第一个镜头(每次都向上舍入)......

>> (("230957".split("").first.to_i + 1).to_s + \
   ("0" * ("230957".size - 1))).to_i
=> 300000

更好(四舍五入正确):

>> (230957 / 10 ** Math.log10(230957).floor) * \
   10 ** Math.log10(230957).floor
=> 200000

It looks a little ugly, but as a first shot (rounds up everytime) ...

>> (("230957".split("").first.to_i + 1).to_s + \
   ("0" * ("230957".size - 1))).to_i
=> 300000

Better (rounds correct):

>> (230957 / 10 ** Math.log10(230957).floor) * \
   10 ** Math.log10(230957).floor
=> 200000
断桥再见 2024-09-19 08:18:14

这是我的版本:

def round(num, nearest = nil, pivot = nil)
  negative = num < 0
  num = -num if negative
  precision = Math.log10(num).to_i rescue 1
  nearest ||= precision == 0 ? 10 : 10**precision
  pivot ||= nearest
  result = (num + pivot) / nearest * nearest
  negative ? -result : result
end

这个方法非常污点,看起来很丑……但是……它处理了一些其他方法不能处理的边缘情况,例如:

  • 以下的0
  • 10 个负数
  • 枢轴点可以更改

以下是一些示例用法:

round(0)   # 0
round(1)   # 10
round(9)   # 10
round(10)  # 20
round(-10) # -20
round(100) # 1000

round(1, 1000)        # 1000
round(499, 1000, 500) # 0
round(500, 1000, 500) # 1000

Here is my version:

def round(num, nearest = nil, pivot = nil)
  negative = num < 0
  num = -num if negative
  precision = Math.log10(num).to_i rescue 1
  nearest ||= precision == 0 ? 10 : 10**precision
  pivot ||= nearest
  result = (num + pivot) / nearest * nearest
  negative ? -result : result
end

This method is very bloted and looks so ugly... BUT... it handles a few edge cases that the others dont such as:

  • 0
  • values under 10
  • negative numbers
  • The pivot point can be changed

Here are some examples of usage:

round(0)   # 0
round(1)   # 10
round(9)   # 10
round(10)  # 20
round(-10) # -20
round(100) # 1000

round(1, 1000)        # 1000
round(499, 1000, 500) # 0
round(500, 1000, 500) # 1000
晨光如昨 2024-09-19 08:18:14

一个简单的建议:

def nearest_large_number value
  str = value.to_s.gsub(/^([0-9])/) { "#{$1}." }
  multiplicator = ("1" + "0" * str.split('.')[1].length).to_i
  str.to_f.ceil * multiplicator
end

使用它:

nearest_large_number 230957
=> 300000

A simple suggestion:

def nearest_large_number value
  str = value.to_s.gsub(/^([0-9])/) { "#{$1}." }
  multiplicator = ("1" + "0" * str.split('.')[1].length).to_i
  str.to_f.ceil * multiplicator
end

To use it:

nearest_large_number 230957
=> 300000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文