SQL 如何在 Oracle PLSQL 中将数字的小数位扩展到最小值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6662153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:17:39  来源:igfitidea点击:

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

sqloracleplsqlformattingnumbers

提问by Stephan Schielke

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.

我想将小数位数扩展到例如 5 个小数位(最小) 如果根本没有小数位,则应该有 5 个零。如果有超过 5 个小数位就可以了。

SELECT ROUND(123,5) FROM DUAL;

will result: 123 instead of 123.00000

将导致:123 而不是 123.00000

The number has a default precision.

该数字具有默认精度。

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

这是可能的还是应该将其转换为具有oracle 数字格式的 varchar ?

I am using Oracle 10g with plsql.

我正在将 Oracle 10g 与 plsql 一起使用。

回答by Vincent Malgrat

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

回答by StevieG

You need to convert it to a Varchar as follows:

您需要将其转换为 Varchar,如下所示:

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

回答by nabuchodonossor

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).

如果要格式化数据,则必须使用 to_char 函数。在数值中尾随零被截断(它们无关紧要)。