Python - 按四分之一间隔舍入

发布于 2024-12-15 02:39:11 字数 361 浏览 1 评论 0原文

我遇到了以下问题:

给出各种数字,例如:

10.38
11.12
5.24
9.76

是否存在一个已经存在的“内置”函数,可以将它们四舍五入到最接近的 0.25 步骤,例如:

10.38 --> 10.50
<代码> 11.12 --> 11.00
<代码> 5.24 --> 5.25
<代码> 9.76 --> 9-75

或者我可以继续编写一个执行所需任务的函数吗?

I'm running into the following issue:

Given various numbers like:

10.38
11.12
5.24
9.76

does an already 'built-in' function exist to round them up to the closest 0.25 step like e.g.:

10.38 --> 10.50
11.12 --> 11.00
5.24 --> 5.25
9.76 --> 9-75

Or can I go ahead and hack together a function that performs the desired task?

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

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

发布评论

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

评论(4

倚栏听风 2024-12-22 02:39:11

这是一个通用解决方案,允许舍入到任意分辨率。对于您的具体情况,您只需提供 0.25 作为分辨率,但其他值也是可能的,如测试用例所示。

def roundPartial (value, resolution):
    return round (value / resolution) * resolution

print "Rounding to quarters"
print roundPartial (10.38, 0.25)
print roundPartial (11.12, 0.25)
print roundPartial (5.24, 0.25)
print roundPartial (9.76, 0.25)

print "Rounding to tenths"
print roundPartial (9.74, 0.1)
print roundPartial (9.75, 0.1)
print roundPartial (9.76, 0.1)

print "Rounding to hundreds"
print roundPartial (987654321, 100)

这输出:

Rounding to quarters
10.5
11.0
5.25
9.75
Rounding to tenths
9.7
9.8
9.8
Rounding to hundreds
987654300.0

This is a general purpose solution which allows rounding to arbitrary resolutions. For your specific case, you just need to provide 0.25 as the resolution but other values are possible, as shown in the test cases.

def roundPartial (value, resolution):
    return round (value / resolution) * resolution

print "Rounding to quarters"
print roundPartial (10.38, 0.25)
print roundPartial (11.12, 0.25)
print roundPartial (5.24, 0.25)
print roundPartial (9.76, 0.25)

print "Rounding to tenths"
print roundPartial (9.74, 0.1)
print roundPartial (9.75, 0.1)
print roundPartial (9.76, 0.1)

print "Rounding to hundreds"
print roundPartial (987654321, 100)

This outputs:

Rounding to quarters
10.5
11.0
5.25
9.75
Rounding to tenths
9.7
9.8
9.8
Rounding to hundreds
987654300.0
失去的东西太少 2024-12-22 02:39:11
>>> def my_round(x):
...  return round(x*4)/4
... 
>>> 
>>> assert my_round(10.38) == 10.50
>>> assert my_round(11.12) == 11.00
>>> assert my_round(5.24) == 5.25
>>> assert my_round(9.76) == 9.75
>>> 
>>> def my_round(x):
...  return round(x*4)/4
... 
>>> 
>>> assert my_round(10.38) == 10.50
>>> assert my_round(11.12) == 11.00
>>> assert my_round(5.24) == 5.25
>>> assert my_round(9.76) == 9.75
>>> 
完美的未来在梦里 2024-12-22 02:39:11

没有内置函数,但这样的函数编写起来很简单

def roundQuarter(x):
    return round(x * 4) / 4.0

There is no builtin, but such a function is trivial to write

def roundQuarter(x):
    return round(x * 4) / 4.0
十秒萌定你 2024-12-22 02:39:11

paxdiablo的解决方案可以稍微改进一下。

def roundPartial (value, resolution):
return round (value /float(resolution)) * resolution

所以该函数现在是:“数据类型敏感”。

The solution of paxdiablo can be a little bit improved.

def roundPartial (value, resolution):
return round (value /float(resolution)) * resolution

so the function is now: "data-type sensitive".

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