关于MySQL中存储类型为Float,使用jdbc查询时丢失精度,或者自动四舍五入的问题


一.   mysql表结构:

 

member_price列的数据类型是float(20,2)(注: 表示最多20位浮点数,精度为2位)

插入记录:

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('1', '1', '2015-11', '5864', '1765910.8745120', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('2', '1', '2015-11', '5864', '1765910.8743120', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('3', '1', '2015-11', '5864', '1765910.874', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('4', '1', '2015-11', '5864', '1765910.879', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('5', '1', '2015-11', '5864', '1765910.871', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('6', '1', '2015-11', '5864', '1765910.87', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('7', '1', '2015-11', '5864', '1765910.8', '1450078966586');

INSERT INTO `member_price` (`id`, `data_type`, `month`, `member_count`, `member_price`, `create_at`) VALUES ('8', '1', '2015-11', '5864', '1765910.1', '1450078966586');

查询:

 

添加条件查询:

 

 注意:

mysql中的float: 借此例说明float(20,2),虽然设置的精度为2位,但是每次插入值时会保存三位,但保存的这三位数是不精确的,如上所示。

查询时

  1. 如果插入是一位小数,但精度是两位,此时插入的数通过相等是查不出来的(除了第一位小数是0和5),因为mysql会将其补全到三位,最终保存的精度也是不准确的

  2. 如过插入是两位小数,并且精度是两位,此时插入的数通过相等也并不一定能查出来

  3. 对于三位小数的查询,同上

所以在mysql中的数值类型,float是不精确的,尽量避免使用,可使用double或者decimal,两者的差别是double是浮点计算,decimal是定点计算,会得到更精确的数据。

二.  jdbc中查询float类型的数据

java代码:

 1 public class Main {
 2     public static void main(String[] args){
 3         Connection connection = null;
 4         PreparedStatement preparedStatement = null;
 5         ResultSet resultSet = null;
 6         try {
 7             Class.forName("com.mysql.jdbc.Driver");
 8             connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user", "root", "123456a");
 9             String sql = "SELECT * from member_price WHERE `month` = '2015-11' and data_type = 1";
10             preparedStatement = connection.prepareStatement(sql);
11             resultSet = preparedStatement.executeQuery();
12             while(resultSet.next()){
13                 System.out.println("string: " + resultSet.getString("member_price"));  //通过String类型获取
14                 System.out.println("float: " + resultSet.getFloat("member_price"));    //通过Long类型获取
15                 System.out.println("============>next");
16             }
17         }catch (Exception e){
18             e.printStackTrace();
19         }finally {
20             try {
21                 resultSet.close();
22                 preparedStatement.close();
23                 connection.close();
24             } catch (SQLException e) {
25                 e.printStackTrace();
26             }
27         }
28 
29     }
30 }
View Code(代码不太规范,只为了演示)

 

截取一部分输出:

string: 1765910.88
float: 1765910.9
============>next
string: 1765910.75
float: 1765910.8
============>next
string: 1765910.12
float: 1765910.1
============>next
string: 1765910.25
float: 1765910.2
============>next
string: 1765910.00
float: 1765910.0
============>next
string: 1765910.38
float: 1765910.4
============>next
string: 1765910.50
float: 1765910.5
============>next
string: 1765910.62
float: 1765910.6
============>next

通过上变测试可以发现:

  1. 数据库中精度设置为2位,通过resultSet.getString()拿到的值是两位(和mysql中查到的是一致的),但通过resultSet.getLong()拿到的值是一位(这一位数是四舍五入的结果(但25最后拿到的是2,不知道为何))

  2. 使用FORMAT(member_price,10),TRUNCATE(member_price,10),ROUND(member_price,10)等函数来查询时,如果通过resultSet.getLong()来获取查询的值,最后也只能获取一位小数,需使用resultSet.getString()获取精确的查询结果

结论: 最好在mysql中不要使用float类型, 对于浮点数的查询,jdbc中最好使用resultSet.getString()获取查询的结果值,resultSet.getLong()只会拿到一位小数

 

若有错误,欢迎批评指正^_^


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM