您还可以使用the to_number() function 的可选第三个参数来指定逗号作为小数分隔符。使用 CTE 提供您的字符串值:
with t (str) as (
select '0,132' from dual
union all select '6,0999999999999999E-2' from dual
)
select to_number(str, '9D9999999999999999EEEE', 'NLS_NUMERIC_CHARACTERS='',.''')
as num
from t;
NUM
----------
.132
.061
格式模型需要在小数点分隔符后有足够多的数字,以确保表格中最精确的数字。 EEEE format 处理科学记数法。
然后您可以使用round() 或trunc() 限制为小数点后三位;舍入与您的样本值没有区别,但截断表明第二个值从 0.061 变为 0.060:
with t (str) as (
select '0,1322' from dual
union all select '6,0999999999999999E-2' from dual
)
select trunc(to_number(str, '9D9999999999999999EEEE',
'NLS_NUMERIC_CHARACTERS='',.'''), 3) as num
from t;
NUM
----------
.132
.06
如果您愿意,可以使用to_char()(或您的客户端/应用程序)显示前导零和尾随零。
您也可以将会话 NLS_NUMERIC_CHARACTERS 设置为 ',.',但更安全的是不要假设运行此查询的任何人都会有特定设置并将其作为查询的一部分。