如何在 SQL 中将小数四舍五入到最接近的偶数?

发布于 2024-09-18 13:20:12 字数 158 浏览 7 评论 0原文

我需要将 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 技术交流群。

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

发布评论

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

评论(3

养猫人 2024-09-25 13:20:12

如果您想将例如四舍五入到第二位小数偶数,您可以执行以下操作: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 produce 3.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 give 3.6.

猫性小仙女 2024-09-25 13:20:12

我真的不明白错误舍入数字的逻辑。此外,这并不容易,因为您不处理整数。但是,如果您确实需要,我建议您遵循类似伪代码的内容。

if ((num / 2) * 2 = num) {
    return // number is even
    }
else {
    num = num + .01 // this assumes you are only working with two decimal points.
}

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.

if ((num / 2) * 2 = num) {
    return // number is even
    }
else {
    num = num + .01 // this assumes you are only working with two decimal points.
}
别念他 2024-09-25 13:20:12

从 Oracle Database 18c 中,您可以使用round_ties_to_even。这会将中间点处的值的最低有效数字四舍五入到最接近的偶数。

第一个参数是要舍入的值。第二个是要舍入的有效位数:

  • 零(默认值)=>整数舍入
  • 正值 =>小数点右侧的位数
  • 负值 =>小数点左边的位数

如果所有值(最多)有两位小数,您甚至可以通过四舍五入到

  • 两位小数将百分位四舍五入到最接近的位数
  • 添加五千位 (0.005 )到输入,因此所有值都位于中点

例如:

with rws as (
  select 8.04 n from dual union all
  select 8.05 n from dual union all
  select 8.06 n from dual union all
  select 8.07 n from dual union all
  select 3.48 n from dual 
)
  select n, round_ties_to_even ( n + 0.005, 2 )
  from   rws;
  
         N ROUND_TIES_TO_EVEN(N+0.005,2)
---------- -----------------------------
      8.04                          8.04
      8.05                          8.06
      8.06                          8.06
      8.07                          8.08
      3.48                          3.48

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:

  • Zero (the default) => integer rounding
  • Positive values => number of digits to the right of the decimal point
  • Negative values => number of digits to the left of the decimal point

If all the values have (at most) two decimal places, you can round the hundredths to the nearest even by

  • Rounding to two decimal places
  • Adding five thousands (0.005) to the input so all values are at the midpoint

For example:

with rws as (
  select 8.04 n from dual union all
  select 8.05 n from dual union all
  select 8.06 n from dual union all
  select 8.07 n from dual union all
  select 3.48 n from dual 
)
  select n, round_ties_to_even ( n + 0.005, 2 )
  from   rws;
  
         N ROUND_TIES_TO_EVEN(N+0.005,2)
---------- -----------------------------
      8.04                          8.04
      8.05                          8.06
      8.06                          8.06
      8.07                          8.08
      3.48                          3.48
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文