如何显示2位小数

lf3rwulv  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(307)

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

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

dced5bon1#

格式化通常是前端的“问题”;每一个合适的工具(无论是oracle apex、reports、forms、jasper reports等等)都有一个选项,可以根据您的需要对数字进行格式化。为什么?因此,这些值仍然是数字,以便能够应用。 SUM 对他们或任何你可能需要的功能。
在sql*plus(或类似的,甚至gui工具)中,格式化是由 TO_CHAR 函数和所需的格式掩码。如果需要两个小数和(例如)千位分隔符,可以执行以下操作:

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 设置掩码的格式,确保在小数点周围看到0。
使用 G 以及 D 表示千组和小数,而不是逗号和点 new_amount2 传统上,有 lpad 应用以使值右对齐。我假设这些值的最大长度是10(您应该更清楚)
如果您确实使用sql*plus(这在现在非常少见),那么您甚至可以使用它的 set numformat 命令so-无需额外修改;您只需选择您拥有的:

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>
xhv8bpkk

xhv8bpkk2#

使用 concat 具体如下:

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

相关问题