關於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-2026 CODEPRJ.COM