Oracle - 用句号表示千位,用逗号表示小数

Oracle - Format number with fullstop for thousand and comma for decimals

我有一个像这样的数字 3225,69,我想将显示转换为:

3.225,69

简单地说,我需要用句号分隔千位,用逗号分隔小数点:

3,69 -> 3,69 
1000 -> 1.000 
1500,20 -> 1.500,20
1 -> 1 (not 1,000)

我应该在 to_char 中使用哪种面膜?

我是我的数据库列,逗号表示小数。

你可以试试这个

select rtrim(to_char(to_char(1, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.'''), 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.'''),',') from dual;

希望对你有帮助

您可以使用 FM format modifier 删除尾随的小数零:

select to_char(1, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.''') from dual;

TO_CHAR(1,'FM9G999G999D999','NLS_NUMERIC_CHARACTERS='',.''')
------------------------------------------------------------
1,      

但是正如您所看到的那样,小数点留在了后面;不过你可以 trim 关闭它:

with t as (
 select 3.69 as n from dual
 union all select 1000 from dual
 union all select 150.20 from dual
 union all select 1 from dual
 union all select 0.16 from dual
)
select n,
  to_char(n, '9G999G999D000') original,
  to_char(n, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.''') new,
  rtrim(to_char(n, 'FM9G999G999D999', 'NLS_NUMERIC_CHARACTERS='',.'''),
    ',') as trimmed
from t;

         N ORIGINAL       NEW            TRIMMED       
---------- -------------- -------------- --------------
      3.69          3.690 3,69           3,69           
      1000      1,000.000 1.000,         1.000          
     150.2        150.200 150,2          150,2          
         1          1.000 1,             1            
       .16           .160 ,16            ,16            

我正在使用 the to_char() function 的可选第三个 NLS 参数来独立于我的会话设置设置 G 和 D 字符。

如果要保留小数点前的零,只需将 D 之前的最后一个 9 变成 0:

with t as (
 select 3.69 as n from dual
 union all select 1000 from dual
 union all select 150.20 from dual
 union all select 1 from dual
 union all select 0.16 from dual
)
select n,
  to_char(n, '9G99G990D000') original,
  to_char(n, 'FM9G999G990D999', 'NLS_NUMERIC_CHARACTERS='',.''') new,
  rtrim(to_char(n, 'FM9G999G990D999', 'NLS_NUMERIC_CHARACTERS='',.'''),
    ',') as trimmed
from t;

         N ORIGINAL      NEW            TRIMMED       
---------- ------------- -------------- --------------
      3.69         3.690 3,69           3,69           
      1000     1,000.000 1.000,         1.000          
     150.2       150.200 150,2          150,2          
         1         1.000 1,             1              
       .16         0.160 0,16           0,16