说oracle,再说oracle的float类型

以前记得遇到过类似的问题。即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时,也会因为精度问题给你自动四舍五入了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值