如何在oracle中使用2位小数进行求值

时间:2018-01-17 16:16:14

标签: oracle rounding

嗨,如果我使用圆函数它有圆形但是如果第三个小数小于5则不会向上舍入。 我的要求是第三个小数第二个小数应该向上舍入。有可能吗?

eg: 17.813 need to be 17.82

20.126 need to be 20.13

Select round(17.813,2) from dual will give 17.81

如何获得这个?

1 个答案:

答案 0 :(得分:3)

您可以将100,use the ceil() function乘以'向上'(种类)调整后的值到最接近的整数,然后再除以100:

ceil(<your number> * 100) / 100

演示:

with t (n) as (
  select 17.813 from dual
  union all select 20.126 from dual
  union all select 1.000 from dual
  union all select 1.001 from dual
  union all select 1.005 from dual
  union all select 1.009 from dual
  union all select 1.010 from dual
)
select n, ceil(n * 100) / 100 as rounded_up
from t;

         N ROUNDED_UP
---------- ----------
    17.813      17.82
    20.126      20.13
         1          1
     1.001       1.01
     1.005       1.01
     1.009       1.01
      1.01       1.01

The round() function使用(对于正数):

ROUND(n, integer) = FLOOR(n * POWER(10, integer) + 0.5) * POWER(10, -integer)

所以你可以用类似的方式概括一个综合版本:

ceil(n * power(10, integer)) * power(10, -integer)

你需要看一下你想如何处理负值,尽管这可能已经按照你的意愿行事了;插入2作为'整数'值:

with t (n) as (
  select 17.813 from dual
  union all select 20.126 from dual
  union all select 1.000 from dual
  union all select 1.001 from dual
  union all select 1.005 from dual
  union all select 1.009 from dual
  union all select 1.010 from dual
  union all select -1.000 from dual
  union all select 0 from dual
  union all select -1.001 from dual
  union all select -1.005 from dual
  union all select -1.009 from dual
  union all select -1.010 from dual
)
select n, ceil(n * power(10, 2)) * power(10, -2) as rounded_up
from t;

         N ROUNDED_UP
---------- ----------
    17.813      17.82
    20.126      20.13
         1          1
     1.001       1.01
     1.005       1.01
     1.009       1.01
      1.01       1.01
        -1         -1
         0          0
    -1.001         -1
    -1.005         -1
    -1.009         -1
     -1.01      -1.01