oracle numer 精度,小数位的关系
在网上看到
带小数位和精度的情况。
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)
还没看明白,到度精度与小算位有什么关系