oracle to_char 进制转换,十进制数,to_char和Oracle

I am trying to figure out a format spec of to_char() that would give me the following result.

to_char(0.1, '[FORMAT_SPEC]')

gives 0.1 and:

to_char(1, '[FORMAT_SPEC]')

gives 1.

I've tried the following solutions:

to_char(0.1)

gives '.1'.

to_char(0.1, 'FM0.099')

gives 0.1, which is okay, however:

to_char(1, 'FM0.099')

gives 1.0, which is not okay.

Do you have any suggestions?

解决方案

The precision returned needs to be consistent, so the only alternative is to use DECODE or CASE statements to conditionally return what you need:

CASE

WHEN INSTR(TO_CHAR(t.col), '.') = 0 THEN TO_CHAR(t.col)

ELSE TO_CHAR(t.col, 'FM0.099')

END

The example isn't great - it's not clear if your data will have values like 1.000 or values above one/etc.

EDIT Michael-O (2013-06-25): For those who need it idiot-proof, you may try:

case

when instr(to_char(

), (select to_char(0, 'FMD') from dual)) = 0

then to_char(

)

else to_char(

, 'FM999990D999')

end

It automatically observes the decimal separator. Adapt the the secodn format modal to your number size.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值