当前位置:网站首页>ETL过程中数据精度不准确问题
ETL过程中数据精度不准确问题
2022-06-26 14:51:00 【RestCloud】
最近一位同学在使用Restcloud ETL产品做数据集成,出现数据传输到目标库表后,数据精度不准确问题。
场景为:从oracle源表数据 格式为:number(21,6)将数据同步到mysql目标表数据格式为:float(21,6) ; 同步数据 发现 oracle是:538121.47 同步到mysql数据库中:538121.50,看到这里,不免有些同学会认为是产品的问题,我们一起来分析下。
首先,我们需要了解下数据在计算机中的区别。在计算机内部,小数有两种表示方法: 定点数和浮点数。
1、浮点型(float和double) 浮点型在数据库中存放的是近似值
MySQL数据类型 含义
float(m,d) 单精度浮点型 8位精度(4字节) m总个数,d⼩数位
double(m,d) 双精度浮点型 16位精度(8字节) m总个数,d⼩数位
设⼀个字段定义为float(5,3),如果插⼊⼀个数123.45678,实际数据库⾥存的是123.457,但总个数还以实际为准,即6位。
2、定点数 定点类型在数据库中存放的是精确值
浮点型在数据库中存放的是近似值,⽽定点类型在数据库中存放的是精确。decimal(m,d) 参数m<65 是总个数,d<30且 d<m 是⼩数位。
对于单精度浮点数Float: 当数据范围在±131072(65536×2)以内的时候,float数据精度是正确的,但是超出这个范围的数据就不稳定,没有发现有相关的参数设置建议:将float改成double或者decimal,两者的差别是double是浮点计算,decimal是定点计算,会得到更精确的数据。
下面使用分析,首先创建测试表
CREATE TABLE customer ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(45) DEFAULT NULL, age int(11) DEFAULT NULL, jinqian float(5,2) DEFAULT NULL, PRIMARY KEY (id) );
float(m,d)
m表示的是最大长度,d表示的显示的小数位数。
例如上面的sql里:float(5,2) 表示:这个浮点数最大长度为5,也就是五位,然后小数部分为2位,至于存储范围,取决于你是否定义了无符号。
无符号的话,最小是0.0 最大能存储到99999.9,如果有符号的话,范围是:-99999.9至99999.9。
默认大小为24位数字,精度大约7位数字(经测试为6位),当设置M大小大于24时,自动转换为DOUBLE类型;同时设置M和D时不进行自动转换。
小数位超过设定值,按四舍五入保存
INSERTINTO customer (id,name,age,jinqian)VALUES(111111111,'uu',15,90.012);
INSERTINTO customer(id,name,age,jinqian)VALUES(1111111111,'uu',15,90.018);
上面两个分别被保存为
总结
从上面的分析,我们可以得出以下结论:
1、浮点数存在误差问题;
2、对货币等对精度敏感的数据,应该用定点数表示或存储;
3、编程中,如果用到浮点数,要特别注意误差问题,并尽量避免做浮点数比较;
4、要注意浮点数中一些特殊值的处理;
边栏推荐
- 备战数学建模30-回归分析2
- The annual salary of 500000 is one line, and the annual salary of 1million is another line
- Principle of TCP reset attack
- 国信证券的排名如何?办理股票开户安全吗?
- Mark: unity3d cannot select resources in the inspector, that is, project locking
- Numpy基本使用
- What is the ranking of Guosen Securities? Is it safe to open a stock account?
- Go变量的声明与赋值
- 房东拿租金去还房贷是天经地义的嘛
- Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
猜你喜欢
![[solo π] ADB connects multiple mobile phones](/img/44/4bd88725434fb67c9237645f024071.png)
[solo π] ADB connects multiple mobile phones

Document 1

Stream常用操作以及原理探索

Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法

Talk about the RPA direction planning: stick to simple and valuable things for a long time

备战数学建模30-回归分析2

View触摸分析

Use abp Zero builds a third-party login module (I): Principles

JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了

Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
随机推荐
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
北京银行x华为:网络智能运维夯实数字化转型服务底座
Idea shortcut key
使用 Abp.Zero 搭建第三方登录模块(二):服务端开发
Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
Where do people get their top energy?
信息学奥赛一本通 1405:质数的和与积 (思维题)
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型
Pod scheduling of kubernetes
TCP 复位攻击原理
Unity UnityWebRequest 下载封装
Numpy基本使用
券商经理给的开户二维码安全吗?找谁可以开户啊?
Informatics Olympiad all in one 1400: count the number of words (string matching)
Redis cluster messages
Is the QR code for account opening given by the manager of the securities firm safe? Who can I open an account with?
Error when redis is started: could not create server TCP listening socket *: 6379: bind: address already in use - solution
Authoritative announcement on the recruitment of teachers in Yan'an University in 2022
人的顶级能量从哪里获取?
Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
