關於MySQL中儲存類型為Float,使用jdbc查詢時丟失精度,或者自動四捨五入的問題

來源:互聯網
上載者:User

標籤:

一.   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()只會拿到一位小數

 

若有錯誤,歡迎批評指正^_^

關於MySQL中儲存類型為Float,使用jdbc查詢時丟失精度,或者自動四捨五入的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.