简体   繁体   English

Oracle中如何在小数点后加零

[英]how to add zeros after decimal in Oracle

I want to add zeroes after the number .我想在数字后添加零。 for eg a= 6895例如 a= 6895
then a= 6895.00那么 a= 6895.00

datatype of a is number(12); a 的数据类型是 number(12);

I am using the below code .我正在使用下面的代码。

select to_char(6895,'0000.00')  from dual .

I m getting the desired result from above code but '6895' can be any number.so due to that i need to add '0' in above code manually.我从上面的代码中得到了想要的结果,但“6895”可以是任何数字。因此,我需要在上面的代码中手动添加“0”。 for eg.例如。

select to_char(68955698,'00000000.00')  from dual .

Can any one suggest me the better method .任何人都可以建议我更好的方法。

The number format models are the place to start when converting numbers to characters. 数字格式模型是将数字转换为字符的起点。 0 prepends 0 s, which means you'd have to get rid of them somehow. 0前置0 s,这意味着您必须以某种方式摆脱它们。 However, 9 means:但是, 9意味着:

Returns value with the specified number of digits with a leading space if positive or with a leading minus if negative.返回具有指定位数的值,如果是正数则带有前导空格,如果是负数则带有前导减号。 Leading zeros are blank, except for a zero value, which returns a zero for the integer part of the fixed-point number.前导零是空白,除了零值,它为定点数的整数部分返回零。

So, the following gets you almost there, save for the leading space:因此,除了领先的空间外,以下内容几乎可以帮助您实现:

SQL> select to_char(987, '9999.00') from dual;

TO_CHAR(
--------
  987.00

You then need to use a format model modifier , FM , which is described thusly:然后,您需要使用格式模型修饰符FM ,其描述如下:

FM Fill mode. FM 填充模式。 Oracle uses trailing blank characters and leading zeroes to fill format elements to a constant width. Oracle 使用尾随空白字符和前导零将格式元素填充为恒定宽度。 The width is equal to the display width of the largest element for the relevant format model宽度等于相关格式模型的最大元素的显示宽度

... ...

The FM modifier suppresses the above padding in the return value of the TO_CHAR function. FM 修饰符抑制 TO_CHAR 函数返回值中的上述填充。

This gives you a format model of fm9999.00 , which'll work:这为您提供了fm9999.00的格式模型, fm9999.00起作用:

SQL> select to_char(987, 'fm9999.00') from dual;

TO_CHAR(
--------
987.00

If you want a lot of digits before the decimal then simply add a lot of 9 s.如果您想要小数点前有很多数字,那么只需添加很多9

datatype of a is number(12); a 的数据类型是 number(12);

Then use 12 9s in the format model.然后在格式模型中使用 12 个 9。 And, keep the decimal to just 2. So, since the column datatype is NUMBER(12) , you cannot have any number more than the given size.并且,将小数保持为 2。因此,由于列数据类型是NUMBER(12) ,因此您不能拥有超过给定大小的任何数字。

SQL> WITH DATA AS(
  2  SELECT 12 num FROM dual union ALL
  3  SELECT 234 num FROM dual UNION ALL
  4  SELECT 9999 num FROM dual UNION ALL
  5  SELECT 123456789 num FROM dual)
  6  SELECT to_char(num,'999999999999D99') FROM DATA
  7  /

TO_CHAR(NUM,'999
----------------
           12.00
          234.00
         9999.00
    123456789.00

SQL>

Update Regarding leading spaces关于前导空格的更新

SQL>  select ltrim(to_char(549,'999999999999.00')) from dual;

LTRIM(TO_CHAR(54
----------------
549.00

SQL>

使用CASESUBSTR非常简单。

CASE WHEN SUBSTR(COLUMN_NAME,1,1) = '.' THEN '0'||COLUMN_NAME ELSE COLUMN_NAME END

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

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