不确定它是否更优雅,但假设您的替换是处理不同的语言环境,这可能对您有用:
with t as (
select 1.00 as n from dual
union all select 1.001 from dual
union all select 0.203 from dual
union all select 0.2345 from dual
union all select 112.999 from dual
)
select n, regexp_replace(to_char(trunc(n, 2), '9999999990D00',
'NLS_NUMERIC_CHARACTERS='',.'''), '[,.]00$', null) as new_n
from t;
N NEW_N
---------- --------------
1 1
1.001 1
0.203 0,20
0.2345 0,23
112.999 112,99
to_char 的 nls_param 参数让您决定它是使用逗号还是句点作为小数分隔符。如果您可以在会话级别进行设置,那么查询看起来会更简单一些。 regexp_replace 从字符串的末尾删除 ,00(或 .00,认为这是矫枉过正)。
正如 ThinkJet 所指出的,regexp_replace 有点过分,并且由于小数分隔符是在列分类中定义的(并且格式无论如何都没有组分隔符),因此可以通过计划 replace 来完成:
with t as (
select 1.00 as n from dual
union all select 1.001 from dual
union all select 0.203 from dual
union all select 0.2345 from dual
union all select 112.999 from dual
union all select 13.08 from dual
)
select n, replace(trim(
to_char(trunc(n, 2), '9999999990D00', 'NLS_NUMERIC_CHARACTERS='',.''')),
',00', null) as new_n
from t;
N NEW_N
---------- --------------
1 1
1.001 1
0.203 0,20
0.2345 0,23
112.999 112,99
13.08 13,08
仍然不确定这是否可以被描述为“优雅”。