以前记得遇到过类似的问题。即ORACLE建立了float字段,后来出了莫名其妙的错误。
这次又遇到了,雷人的。我被害惨了,以后谁要在ORACLE里用float类型,我就跟他拼命。
把原理写在这里,供以后查询,我专门在ORACLE里做了个实验,把脚本贴在这里。
Oracle Online Help 说:
FLOAT(b) specifies a floating-point number with binary precision b. The precision b can range from 1 to 126. To convert from binary to decimal precision, multiply b by 0.30103.
意思是说oracle 的float类型,是按二进制来算的精度,其中精度范围为二进制的1到126,在转化为我们常说的进十进制是,需要X以0.30103
注意它说的是binary pricision而不是decimal precision
你指定3,按照换算公式 INT(3*0.30103)=0,所以小数点后精度为0。
如果需要精度为3,b = CEIL(3 / 0.30103) = 10。
另外。根据我实际测试,ORACLE说的这个精度,还是包含了整数的。
示例脚本:
drop table testdatatype;
--verify the floating-point number with binary precision
create table testdatatype(
id number(4),
a float(3), --1/0.30103
b float(4),
c float(6), --2/0.30103
d float(7),
e float(9), --3/0.30103
f float(10),
g float(14), --4/0.30103
h float (15), --5/0.30103
i float(16), --6/0.30103
j float(24), --7/0.30103
k float(27) --8/0.30103
);
insert into testdatatype values(
1,
999.999
,999.999
,999.999
,999.999
,999.999
,999.999
,999.999
,999.999
,999.999
,999.999
,999.999
);
insert into testdatatype values(
2,
999.555
,999.555
,999.555
,999.555
,999.555
,999.555
,999.555
,999.555
,999.555
,999.555
,999.555
);
insert into testdatatype values(
3,
999.455
,999.455
,999.455
,999.455
,999.455
,999.455
,999.455
,999.455
,999.455
,999.455
,999.455
);
insert into testdatatype values(
4,
999.444
,999.444
,999.444
,999.444
,999.444
,999.444
,999.444
,999.444
,999.444
,999.444
,999.444
);
insert into testdatatype values(
5,
999.111
,999.111
,999.111
,999.111
,999.111
,999.111
,999.111
,999.111
,999.111
,999.111
,999.111
);
insert into testdatatype values(
6,
1942070
,1942070
,1942070
,1942070
,1942070
,1942070
,1942070
,1942070
,1942070
,1942070
,1942070
);
commit;
来看结果。
ID A B C D E F G H I J K
1 1 1000 1000 1000 1000 1000 1000 1000 1000 1000 999.999 999.999
2 2 1000 1000 1000 1000 1000 999.6 999.56 999.56 999.56 999.555 999.555
3 3 1000 1000 1000 999 999 999.5 999.46 999.46 999.46 999.455 999.455
4 4 1000 1000 1000 999 999 999.4 999.44 999.44 999.44 999.444 999.444
5 5 1000 1000 1000 999 999 999.1 999.11 999.11 999.11 999.111 999.111
6 6 2000000 1900000 1900000 1940000 1940000 1942000 1942100 1942100 1942100 1942070 1942070
注意到最后一排,就算你写的整数。在做INSERT时,也会因为精度问题给你自动四舍五入了。