如何在 SQL 中将小数四舍五入到最接近的偶数?
我需要将 Oracle 10g 上的 SQL 查询中的小数四舍五入为最接近的偶数。 如果数字是偶数,则应返回。如果数字是奇数,则应返回下一个偶数。
这就是我想要的: 8.05 应该返回 8.06,3.48 应该返回 3.48
我该怎么做?
谢谢, 安德鲁
I need to round a decimal in a sql query on Oracle 10g to the nearest even number.
If the number is even, it should be returned. If the number is odd, the next even number should be returned.
This is what I want:
8.05 should return 8.06, 3.48 should return 3.48
How can I do this?
Thanks,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想将例如四舍五入到第二位小数偶数,您可以执行以下操作:
select round(3.43 / 0.02, 0) * 0.02;
这将产生3.44
。这可以根据您的意愿进行扩展:例如,第一个十进制数字是 3 的倍数:
select round(3.5452234 / 0.3, 0) * 0.3;
将给出3.6
。If you want to round e.g. to the second decimal even digit, you can do something like that:
select round(3.43 / 0.02, 0) * 0.02;
that will produce3.44
.This can be extended as you wish: e.g. first decimal digit which is multiple of 3:
select round(3.5452234 / 0.3, 0) * 0.3;
will give3.6
.我真的不明白错误舍入数字的逻辑。此外,这并不容易,因为您不处理整数。但是,如果您确实需要,我建议您遵循类似伪代码的内容。
I really don't understand the logic of incorrectly rounding numbers. Additionally, this cannot easily because you aren't dealing with integers. However, if you really need to, I would suggest following something like this pseudo-code.
从 Oracle Database 18c 中,您可以使用
round_ties_to_even
。这会将中间点处的值的最低有效数字四舍五入到最接近的偶数。第一个参数是要舍入的值。第二个是要舍入的有效位数:
如果所有值(最多)有两位小数,您甚至可以通过四舍五入到
例如:
From Oracle Database 18c you can use
round_ties_to_even
. This rounds the least significant digit for values at the half-way point to the nearest even number.The first parameter is the value to round. The second is the number of significant digits to round to:
If all the values have (at most) two decimal places, you can round the hundredths to the nearest even by
For example: