我想在Oracle中对数字进行舍入,只保留逗号后的2位数,仅跟随第三个小数的值。 我想要的例子:
1,374 --> 1,37
1,375 --> 1,37 (the 5 need to be rounding down)
1,3756 --> 1,37 (We only look the third decimal, even if the 4th is a 6)
1,376 --> 1,38
你会怎么做? 我检查了ROUND()函数,但行为不是我需要的。
感谢您的帮助
答案 0 :(得分:1)
将round
与floor
或trunc
结合使用,如下所示:
with t as (
select column_value val
from table (sys.odcinumberlist(1.374, 1.375, 1.3756, 1.376)))
select val,
round(trunc(val, 3)-.0001, 2) v1,
round((floor(val*1000)-1)/1000, 2) v2
from t
输出:
VAL V1 V2
---------- ---------- ----------
1,374 1,37 1,37
1,375 1,37 1,37
1,3756 1,37 1,37
1,376 1,38 1,38
答案 1 :(得分:1)
您可以稍微调整您正在四舍五入的值,例如:
round(trunc(<your number>, 3) - 0.001, 2)
trunc(<your number>, 3)
表示忽略第三个小数后的所有内容,因此1,3756将被视为与1,375相同。 - 0.001
然后稍微调整该截断值,以便正常round()
行为将使得上下之间的临界点看起来是.x6而不是.x5。
快速演示:
alter session set nls_numeric_characters =',.';
with t (n) as (
select 1.37 from dual
union all select 1.374 from dual
union all select 1.374999 from dual
union all select 1.375 from dual
union all select 1.375001 from dual
union all select 1.3756 from dual
union all select 1.375999 from dual
union all select 1.376 from dual
union all select 1.37999 from dual
)
select n, round(n, 2) as simple, trunc(n, 3) as tmp1, trunc(n, 3) - 0.001 as tmp2,
round(trunc(n, 3) - 0.001, 2) as adjusted
from t;
N SIMPLE TMP1 TMP2 ADJUSTED
---------- ---------- ---------- ---------- ----------
1,37 1,37 1,37 1,369 1,37
1,374 1,37 1,374 1,373 1,37
1,374999 1,37 1,374 1,373 1,37
1,375 1,38 1,375 1,374 1,37
1,375001 1,38 1,375 1,374 1,37
1,3756 1,38 1,375 1,374 1,37
1,375999 1,38 1,375 1,374 1,37
1,376 1,38 1,376 1,375 1,38
1,37999 1,38 1,379 1,378 1,38