8

在转换数字时需要一些帮助:

select to_char(a, '99D99')
       , to_char(a, '90D99')
from
(
select 50 a from dual
union
select 50.57 from dual
union
select 5.57 from dual
union
select 0.35 from dual
union
select 0.4 from dual

将导致:

1      ,35    0,35
2      ,40    0,40
3     5,57    5,57
4    50,00   50,00
5    50,57   50,57

但是如何使我的输出如下:

  1. 0,35
  2. 0,4
  3. 5,57
  4. 50
  5. 50,57

我需要0逗号之前,但不是之后。

4

2 回答 2

13

使用FM格式模型修饰符来接近,因为你不会得到小数分隔符后的尾随零;但你仍然会得到分隔符本身,例如50.. 你可以用rtrim它来摆脱它:

select to_char(a, '99D90'),
    to_char(a, '90D90'),
    to_char(a, 'FM90D99'),
    rtrim(to_char(a, 'FM90D99'), to_char(0, 'D'))
from (
    select 50 a from dual
    union all select 50.57 from dual
    union all select 5.57 from dual
    union all select 0.35 from dual
    union all select 0.4 from dual
)
order by a;

TO_CHA TO_CHA TO_CHA RTRIM(
------ ------ ------ ------
   .35   0.35 0.35   0.35
   .40   0.40 0.4    0.4
  5.57   5.57 5.57   5.57
 50.00  50.00 50.    50
 50.57  50.57 50.57  50.57

请注意,我to_char(0, 'D')用于生成要修剪的字符,以匹配小数点分隔符 - 因此它会查找相同的字符,,或者.,作为第一个to_char添加的字符。

The slight downside is that you lose the alignment. If this is being used elsewhere it might not matter, but it does then you can also wrap it in an lpad, which starts to make it look a bit complicated:

...
lpad(rtrim(to_char(a, 'FM90D99'), to_char(0, 'D')), 6)
...

TO_CHA TO_CHA TO_CHA RTRIM( LPAD(RTRIM(TO_CHAR(A,'FM
------ ------ ------ ------ ------------------------
   .35   0.35 0.35   0.35     0.35
   .40   0.40 0.4    0.4       0.4
  5.57   5.57 5.57   5.57     5.57
 50.00  50.00 50.    50         50
 50.57  50.57 50.57  50.57   50.57
于 2013-04-08T10:59:51.103 回答
0

这应该可以解决您的问题:

select replace(to_char(a, '90D90'),'.00','')
from
(
select 50 a from dual
union
select 50.57 from dual
union
select 5.57 from dual
union
select 0.35 from dual
union
select 0.4 from dual
);

也可以看看这个SQL Fiddle进行测试。

于 2013-04-08T09:34:57.527 回答