繁体   English   中英

Oracle SQL - 如何显示 2 个小数位

[英]Oracle SQL - How to display 2 decimal places

帮我显示一个长度未知的数字,小数点后 2 位。 示例:如果数字是 230000,那么我需要将其打印为 230000.00

我试过了

Select to_char(amount, 'FM99999999.90') as amt from customer;

格式化通常是前端的“问题”; 每个像样的工具(无论是 Oracle Apex、报告、Forms、Jasper Reports 等)都可以根据需要设置数字格式。 为什么? 这样这些值仍然是numbers ,以便能够将SUM function 应用于它们或您可能需要的任何东西。

在 SQL*Plus(或类似的,甚至是 GUI 工具)中,格式化由TO_CHAR function 和所需的格式掩码完成。 如果您想要两位小数和(例如)千位分隔符,您可以执行以下操作:

SQL> with customer (amount) as
  2    (select 230000 from dual union all
  3     select 3.14   from dual union all
  4     select 0.0002 from dual union all
  5     select 25.123 from dual
  6    )
  7  select amount,
  8         to_char(amount, 'fm999g999g990d00') new_amount,
  9    lpad(to_char(amount, 'fm999g999g990d00'), 10, ' ') new_amount2
 10  from customer;

    AMOUNT NEW_AMOUNT      NEW_AMOUNT2
---------- --------------- ----------------------------------------
    230000 230.000,00      230.000,00
      3,14 3,14                  3,14
     ,0002 0,00                  0,00
    25,123 25,12                25,12

SQL>
  • 请注意,新值具有... 0d00格式掩码,可确保您实际上会在小数点周围看到
  • 使用GD表示千组和小数,而不是逗号和点
  • new_amount2 ,另外,已应用lpad以便值右对齐。 我假设这些值的最大长度是 10(你会知道的更好)

如果您确实使用 SQL*Plus(现在很少见),您甚至可以使用它的set numformat命令 - 无需额外修改; 你只是 select 你有什么:

SQL> set numformat 999g990d00
SQL>
SQL> with customer (amount) as
  2    (select 230000 from dual union all
  3     select 3.14   from dual union all
  4     select 0.0002 from dual union all
  5     select 25.123 from dual
  6    )
  7  select amount
  8  from customer;

     AMOUNT
-----------
 230.000,00
       3,14
       0,00
      25,12

SQL>

使用concat如下:

select concat(amount, '.00') from customer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM