简体   繁体   English

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

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

Just help me out to display a number whose length is unknown with 2 decimal places.帮我显示一个长度未知的数字,小数点后 2 位。 Example: If the number is 230000 then i need it to be printed as 230000.00示例:如果数字是 230000,那么我需要将其打印为 230000.00

I tried我试过了

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

Formatting is usually front-end "problem";格式化通常是前端的“问题”; every decent tool (be it Oracle Apex, Reports, Forms, Jasper Reports, ...) has an option to format a number as you want it.每个像样的工具(无论是 Oracle Apex、报告、Forms、Jasper Reports 等)都可以根据需要设置数字格式。 Why?为什么? So that those values remain numbers , to be able to apply eg SUM function to them or whatever you may need.这样这些值仍然是numbers ,以便能够将SUM function 应用于它们或您可能需要的任何东西。

In SQL*Plus (or similar, even GUI tools), formatting is done by the TO_CHAR function and desired format mask.在 SQL*Plus(或类似的,甚至是 GUI 工具)中,格式化由TO_CHAR function 和所需的格式掩码完成。 If you want two decimals and (just for example) thousands separator, you might do something like this:如果您想要两位小数和(例如)千位分隔符,您可以执行以下操作:

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>
  • note that new values have... 0d00 format mask which makes sure you'll actually see zeroes around the decimal point.请注意,新值具有... 0d00格式掩码,可确保您实际上会在小数点周围看到
  • use G and D for thousand groups and decimals, rather than commas and dots使用GD表示千组和小数,而不是逗号和点
  • new_amount2 , aditionaly, has lpad applied so that values are right-aligned. new_amount2 ,另外,已应用lpad以便值右对齐。 I presumed that max length of those values is 10 (you'd know better)我假设这些值的最大长度是 10(你会知道的更好)

If you do use SQL*Plus (which is quite rare nowadays), you could even use its set numformat command so - no additional modifications are necessary;如果您确实使用 SQL*Plus(现在很少见),您甚至可以使用它的set numformat命令 - 无需额外修改; you just select what you have:你只是 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>

Use concat as follows:使用concat如下:

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

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

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