您的位置:首页 > 数据库 > Oracle

oracle number类型的格式化输出

2011-04-08 07:12 435 查看
问题:

输入的浮点数与小数位,
input   output
------- ---------
0.123      0.12
.123      0.12
.1      0.10
.199      0.20
123.199  123.1990
 

解法:

create table test_t(
col_1 number(3,2),
col_2 number(7,4)
);
insert into test_t(col_1) values(0.123);
insert into test_t(col_1) values(.123);
insert into test_t(col_1) values(.1);
insert into test_t(col_1) values(.199);
insert into test_t(col_2) values(123.199);
//
1.直接查询数据,就能显示效果
x number(p,s):
p:x的有效位数,既x的宽度
s:x的小数位数
x的有效位数计算:
s>=0,x的有效位:p
s<0,x的有效位:p+|s|
select * from test_t;
COL_1     COL_2
----- ---------
0.12          /*因为小数点只是2位,在进行存储时进行了四舍五入*/
0.12          /*因为小数点只是2位,在进行存储时进行了四舍五入,当整数部分空缺时,在数据最左边补齐一个0*/
0.10          /*当实际值的小数位数小于指定位数时,在数字最右边补齐0*/
0.20          /*存储时进行了四舍五入*/
123.1990
//
2.使用to_char()函数格式输出
SQL> select nvl(to_char(col_1,990.99),'Unknow') col_1,
2         nvl(to_char(col_2,990.9999),'Unknow') col_2
3         from test_t;
COL_1   COL_2
------- ---------
0.12 Unknow
0.12 Unknow
0.10 Unknow
0.20 Unknow
Unknow   123.1990
 

原帖:http://topic.csdn.net/u/20110314/11/6459c365-ea61-4942-a22a-3a8a11c32899.html?seed=79231246&r=72139222#r_72139222

更多关于number类型,请参考:

1.http://www.ixora.com.au/notes/numeric_datatypes.htm

2.http://www.ixora.com.au/notes/number_representation.htm?number=12.00#example

3.http://blog.csdn.net/BOBO12082119/archive/2011/03/01/6215763.aspx

4.http://topic.csdn.net/u/20110307/13/e168a4d6-a5e6-434c-ac65-4ea620d7da12.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息