How to expand decimal places of a number to a minimum in Oracle PLSQL?

久未见 提交于 2019-12-23 12:35:01

问题


I cant figure out how to select the following:

123        -> 123.00000
123.12     -> 123.12000
123.123456 -> 123.123456

I would like to expand the number of decimal places to for example 5 decimal places (minimum) If there are no decimal places at all there should be 5 zeros. It is fine if there are more then 5 decimal places.

SELECT ROUND(123,5) FROM DUAL;

will result: 123 instead of 123.00000

The number has a default precision.

Is this possible or should I convert it to a varchar with the oracle number formats?

I am using Oracle 10g with plsql.


回答1:


You could use the following:

SQL> SELECT X, to_char(X, 'fm99999999.00000999')
  2    FROM (SELECT 123 X FROM dual UNION ALL
  3          SELECT 123.12 FROM dual UNION ALL
  4          SELECT 123.123456 FROM dual);

         X TO_CHAR(X,'FM99999999.00000999
---------- ------------------------------
       123 123.00000
    123.12 123.12000
123.123456 123.123456



回答2:


You need to convert it to a Varchar as follows:

SELECT to_char(123, '9999.99999') from dual;



回答3:


if you want to FORMAT the data, you have to use the to_char function. in numeric values trailing zeroes are truncated (they are irrelevant).



来源:https://stackoverflow.com/questions/6662153/how-to-expand-decimal-places-of-a-number-to-a-minimum-in-oracle-plsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!