oracle numer 精度,小数位的关系

fuyou001 2008-11-26 02:46:20
在网上看到


带小数位和精度的情况。
SQL> create table test_number2(col_char varchar(20),col_num number(1,3));

Table created
精度是1,小数位是3.
可见,精度不是小数位加整数位了。但是精度和小数位倒底什么关系呢?
SQL> insert into test_number2 values('0.111',0.111);

insert into test_number2 values('0.111',0.111)

ORA-01438: value larger than specified precision allowed for this column
插入3位小数,0.111竟然报错了,说精度不够。
SQL> insert into test_number2 values('0.001',0.001);

1 row inserted
插入0.001时,成功了。
SQL> insert into test_number2 values('0.001',0.0015);

1 row inserted
插入0.0015也成功了。
看看插入的值。
SQL> select * from test_number2;

COL_CHAR COL_NUM
-------------------- -------
0.001 0.001
0.0015 0.002
需要注意的是0.0015被舍入为0.002

精度大于小数位
SQL> create table test_number3 (col_char varchar(20), col_number number(5,3));

Table created

SQL> insert into test_number3 values('99.899',99.899);

1 row inserted

SQL> insert into test_number3 values('99.999',99.999);

1 row inserted

SQL> insert into test_number3 values('99.9999',99.9999);

insert into test_number3 values('99.9999',99.9999)

ORA-01438: value larger than specified precision allowed for this column

注意,当插入99.9999时,系统报错。因为小数位为3位。第四位小数位是9,于是往前入。最终变成100.000.就已经超过了精度。
Number(5,3)可存储的数值最大为99.999.

--总结
现在终于有点明白小数位与精度的关系了。
现在应该明白了精度和小数位的关系了吧。
小数位告诉系统保留多少位小数,从哪里开始舍入。
精度舍入后,从舍入的位置开始,数值中允许有多少位(这里怎么理解):我的理解是:小数位四舍五入后,从舍入位置计数位数,但这与(插入0.001时,成功了。
SQL> insert into test_number2 values('0.001',0.0015);

1 row inserted
插入0.0015也成功了。
)矛盾啊(这里的位数是5啊,而精度是1,不是位数不等于精度啊)
找了api看了下:
NUMBER(p,s)

where:

p is the precision, or the total number of digits. Oracle guarantees the portability of numbers with precision of up to 38 digits.

s is the scale, or the number of digits to the right of the decimal point. The scale can range from -84 to 127.

Specify an integer using the following form:

NUMBER(p)

还没看明白,到度精度与小算位有什么关系
...全文
1452 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
范佩西_11 2008-11-26
  • 打赏
  • 举报
回复
number(1,3)插入0.001,0.00312等等都是对的
当number(n,m)n<m时
(m-n)位必须是0,精度是取到m位的。
SQL> create table aaa (a number(2,6));

Table created

SQL> insert into aaa values(0.00123);

insert into aaa values(0.00123)

ORA-01438: 值大于为此列指定的允许精度

SQL> insert into aaa values(0.0000123);

1 row inserted

SQL> insert into aaa values(0.0000123345345);

1 row inserted
fuyou001 2008-11-26
  • 打赏
  • 举报
回复
我看出来有什么区别啊
wiler 2008-11-26
  • 打赏
  • 举报
回复
5楼的测试有道理
fuyou001 2008-11-26
  • 打赏
  • 举报
回复
谢谢两位,我是菜鸟一个,谈不研究,只是钻进了牛角
codearts 2008-11-26
  • 打赏
  • 举报
回复
楼主很钻研啊,呵呵。 我也没搞明白, 不过我这里有个测试,或许给你点提示

scott@ORA1> select dump(0.001) from dual;

DUMP(0.001)
-------------------
Typ=2 Len=2: 191,11

scott@ORA1> select dump(0.005) from dual;

DUMP(0.005)
-------------------
Typ=2 Len=2: 191,51

scott@ORA1> select dump(0.111) from dual;

DUMP(0.111)
----------------------
Typ=2 Len=3: 192,12,11
BlueskyWide 2008-11-26
  • 打赏
  • 举报
回复
--给字段宽度定义大一点即可:

create table test_number2(col_char varchar(20),col_num number(15,3));




[Quote=引用楼主 fuyou001 的帖子:]
在网上看到


带小数位和精度的情况。
SQL> create table test_number2(col_char varchar(20),col_num number(1,3));

Table created
精度是1,小数位是3.
可见,精度不是小数位加整数位了。但是精度和小数位倒底什么关系呢?
SQL> insert into test_number2 values('0.111',0.111);

insert into test_number2 values('0.111',0.111)

ORA-01438: value larger than specified precision allowed for this…
[/Quote]
fuyou001 2008-11-26
  • 打赏
  • 举报
回复
那为什么
SQL> insert into test_number2 values('0.001',0.001);

1 row inserted
插入0.001时,成功了。
hebo2005 2008-11-26
  • 打赏
  • 举报
回复
如果你写number(1,3)
然后插入0.003那肯定报错
前面的1表示数字最多一位,0.003有四位
要写成number(4,3)才能插入
hebo2005 2008-11-26
  • 打赏
  • 举报
回复
首先精度是应该>=小数位

设number(10,5)
表示,这个数最大是10位,其中整数位5位,小数位5位

17,377

社区成员

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

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