请教ORACLE中如何将数字转化为字符串

接口一混 2010-07-28 02:23:58
本人为oracle菜鸟,
select 500 / 1000 from dual
显示:0.5
select to_char(500 / 1000) from dual
显示:.5

我想请教,ORACLE中如何将数字转化为字符串能够正确显示出0.5

例:select to_char(500 / 1000,'0.0') from dual
这样结果是为 0.5
但是当select to_char(5000 / 1000,'0.0') from dual
这是的结果是 5.0 (这样是有问题的,我需要的是5不要后面的.0)

但是当select to_char(5000 / 1000,'0.00') from dual
这是的结果是 5.00 (这样是有问题的,我需要的是5不要后面的.00)

但是当select to_char(500 / 1000,'0.00') from dual
这是的结果是 0.50(这样是有问题的,我需要先显示的是0.5而不是0.50)
请教该如何实现呢?
...全文
6866 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
heyuelovemeng 2012-01-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 lyg1761983 的回复:]

SQL code
select rtrim(to_char(5,'fm0.99'),'.') from dual;
select rtrim(to_char(.5,'fm0.99'),'.') from dual;
select rtrim(to_char(.05,'fm0.99'),'.') from dual;
[/Quote]

这样也是不正确的啊
select rtrim(to_char(500000/10000,'fm0.99'),'.') from dual;
结果:#####
select rtrim(to_char(500/100000,'fm0.99'),'.') from dual;
结果:0.01
lyg1761983 2012-01-06
  • 打赏
  • 举报
回复
select rtrim(to_char(5,'fm0.99'),'.') from dual;
select rtrim(to_char(.5,'fm0.99'),'.') from dual;
select rtrim(to_char(.05,'fm0.99'),'.') from dual;
lyg1761983 2012-01-06
  • 打赏
  • 举报
回复
select rtrim(to_char(5,'fm0.99'),'.') from dual;
select rtrim(to_char(.5,'fm0.99'),'.') from dual;
select rtrim(to_char(.05,'fm0.99'),'.') from dual;
qldsrx 2010-07-28
  • 打赏
  • 举报
回复
select rtrim(to_char(5,'fm0.99'),'.') from dual;
select rtrim(to_char(.5,'fm0.99'),'.') from dual;
select rtrim(to_char(.05,'fm0.99'),'.') from dual;
beita258 2010-07-28
  • 打赏
  • 举报
回复
select to_char(decode(substr(to_char(round(5000/1000,1)),1,1),'.','0'||round(5000/1000,1),round(5000/1000,1))) from dual

select to_char(decode(substr(to_char(round(500/1000,1)),1,1),'.','0'||round(500/1000,1),round(500/1000,1))) from dual
cqhweb 2010-07-28
  • 打赏
  • 举报
回复
TO_CHAR (number)


Text description of to_char_number


Purpose
TO_CHAR (number) converts n of NUMBER datatype to a value of VARCHAR2 datatype, using the optional number format fmt. If you omit fmt, then n is converted to a VARCHAR2 value exactly long enough to hold its significant digits.

The 'nlsparam' specifies these characters that are returned by number format elements:

Decimal character
Group separator
Local currency symbol
International currency symbol
This argument can have this form:

'NLS_NUMERIC_CHARACTERS = ''dg''
NLS_CURRENCY = ''text''
NLS_ISO_CURRENCY = territory '


The characters d and g represent the decimal character and group separator, respectively. They must be different single-byte characters. Within the quoted string, you must use two single quotation marks around the parameter values. Ten characters are available for the currency symbol.

If you omit 'nlsparam' or any one of the parameters, then this function uses the default parameter values for your session.

See Also:
"Format Models" for information on number formats


Examples
The following statement uses implicit conversion to interpret a string and a number into a number:

SELECT TO_CHAR('01110' + 1) FROM dual;

TO_C
----
1111


Compare this example with the first example for TO_CHAR (character).

In the next example, the output is blank padded to the left of the currency symbol.

SELECT TO_CHAR(-10000,'L99G999D99MI') "Amount"
FROM DUAL;


Amount
--------------
$10,000.00-

SELECT TO_CHAR(-10000,'L99G999D99MI',
'NLS_NUMERIC_CHARACTERS = '',.''
NLS_CURRENCY = ''AusDollars'' ') "Amount"
FROM DUAL;


Amount
-------------------
AusDollars10.000,00-


--------------------------------------------------------------------------------
Note:
In the optional number format fmt, L designates local currency symbol and MI designates a trailing minus sign. See Table 2-13 for a complete listing of number format elements.

archwuke1 2010-07-28
  • 打赏
  • 举报
回复
呵呵,以上办法没考虑到结果为负的情况

请无视,不好意思
archwuke1 2010-07-28
  • 打赏
  • 举报
回复
我试了一下没想到直接达成的办法
不过其实冲突就是在不使用fmt参数,并且结果小于的时候没显示小数点前的0
这样不如在小于的时候直接加个0在前面好了 ^_^

比如我想到的最笨的办法:
SQL> select decode(sign(500/1000-1),-1,'0'||to_char(500/1000),to_char(500/1000)) from dual;

DECODE(SIGN(500/1000-1),-1,'0'
------------------------------
0.5

SQL> select decode(sign(5000/1000-1),-1,'0'||to_char(5000/1000),to_char(5000/1000)) from dual;

DECODE(SIGN(5000/1000-1),-1,'0
------------------------------
5
接口一混 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 phoenix_99 的回复:]
SQL code
select to_number(to_char(50000 / 10000,'000.00000')) from dual
[/Quote]

呵呵,我是要把数字转化为字符串啊
接口一混 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 java3344520 的回复:]
select to_char(500 / 1000,'fm90.99') from dual;

TO_CHAR(500/1000,'FM90.99')
---------------------------
0.5
这样说比较理想的情况...
[/Quote]
select to_char(50 / 10000,'fm90.99') from dual;
结果:0.01 不对啊
select to_char(300 / 10,'fm90.99') from dual;
结果:30. 不对啊
select to_char(3000 / 10,'fm90.99') from dual;
结果:###### 不对啊
请高手们再给看一下吧


接口一混 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beita258 的回复:]
select round(500/1000,1) from dual
select round(5000/1000,1) from dual
[/Quote]

select to_char(round(500/1000,3)) from dual
结果:.5 还是不对啊
Phoenix_99 2010-07-28
  • 打赏
  • 举报
回复
select to_number(to_char(50000 / 10000,'000.00000')) from dual
beita258 2010-07-28
  • 打赏
  • 举报
回复
select round(500/1000,1) from dual
select round(5000/1000,1) from dual
iqlife 2010-07-28
  • 打赏
  • 举报
回复
select to_char(500 / 1000,'fm90.99') from dual;

TO_CHAR(500/1000,'FM90.99')
---------------------------
0.5
这样说比较理想的情况...
iqlife 2010-07-28
  • 打赏
  • 举报
回复
/*
FM :除空格
9999999.0099:允许小数点左边最大正数为7位,小数点右边最少2位,最多4位,且在第5位进行四舍五入
*/
Select TO_CHAR(123.0233,'FM9999999.0099') FROM DUAL

例如
SQL> select to_char(5000 / 1000,'fm9.99') from dual;

TO_CHAR(5000/1000,'FM9.99')
---------------------------
5.

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧