简体   繁体   English

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

[英]How can I round a decimal to the nearest even number in SQL?

I need to round a decimal in a sql query on Oracle 10g to the nearest even number.我需要在 Oracle 10g 上的 sql 查询中将小数四舍五入到最接近的偶数。 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这就是我想要的:8.05 应该返回 8.06,3.48 应该返回 3.48

How can I do this?我怎样才能做到这一点?

Thanks, Andrew谢谢,安德鲁

If you want to round eg to the second decimal even digit, you can do something like that: select round(3.43 / 0.02, 0) * 0.02; 如果要舍入到第二个十进制偶数位,则可以执行以下操作: select round(3.43 / 0.02, 0) * 0.02; that will produce 3.44 . 这将产生3.44

This can be extended as you wish: eg first decimal digit which is multiple of 3: select round(3.5452234 / 0.3, 0) * 0.3; 可以根据需要进行扩展:例如,第一个十进制数字为3的倍数: select round(3.5452234 / 0.3, 0) * 0.3; will give 3.6 . 将给3.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. 但是,如果您确实需要,我建议您遵循类似此伪代码的内容。

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

From Oracle Database 18c you can use round_ties_to_even .从 Oracle 数据库 18c 中,您可以使用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零(默认)=> integer 舍入
  • 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将五千 (0.005) 添加到输入,因此所有值都在中点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM