简体   繁体   English

mysql float类型

[英]mysql float type

I have a database order with price and deposit fields set to float type. 我有一个pricedeposit字段设置为浮点型的数据库order I am also implemeting a java gui to search the order, the problem is when I try to search for the orders in the database I dont find anything because it saves price=55.0 when I convert from string to float and in the database it saves as 55. What is the problem? 我还实现了一个Java gui来搜索订单,问题是当我尝试在数据库中搜索订单时我什么也找不到,因为当我从字符串转换为float并在数据库中保存为时,它节省了price = 55.0 55.什么问题? what type should I use to represent currency from the two sides, java side and mysql side? 我应该使用哪种类型来表示Java和MySQL方面的货币?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {        
    try{
        //collect arguments
        String category = this.jTextField8.getText();
        String pattern=this.jTextArea1.getText();
        String color=this.jTextArea2.getText();
        float price = Float.valueOf(this.jTextField14.getText()).floatValue();
        float deposit = Float.valueOf(this.jTextField15.getText()).floatValue();

        //create new order
        int row=this.customerSelectedRow;
        Order newOrder=new order(this.customerModel.dataVector.get(row),category,pattern,color,price,deposit);
        newOrder.search();           
        //refresh
        this.jDialogNewOrder.setVisible(false);

    }catch(Exception e){
         System.err.println (" Error message: " + e.getMessage ());
    }

}                     

here is the code for the search method 这是搜索方法的代码

try {
                s = c.conn.prepareStatement("SELECT id FROM `"+this.table+
                        "` WHERE customerId=? AND category=? and pattern=? and color=? and deposit=? and price=?");
                s.setInt(1,this.customer.getId());
                s.setString (2, this.category);
                s.setString (3, this.pattern);
                s.setString (4, this.color);
                s.setFloat(5,this.deposit);
                s.setFloat(6,this.price);
                System.err.println(s.toString());
                ResultSet res = s.executeQuery();

                System.out.println("printing ids :");
                while (res.next()) {
                  int i = res.getInt(1);
                  //assigning the correct id to the inserted customer
                  this.id=i;
                  System.out.println("id" + "=" + i);
                }
            } catch (SQLException e) {
                System.err.println ("find id Error message: " + e.getMessage ());
                System.err.println ("find id Error number: " + e.getErrorCode ());

Are you dealing with currency with floating point values? 您正在处理具有浮点值的货币吗?

Please don't. 请不要

Floating point values cannot represent exact decimal values, as they are representation of binary fractions. 浮点值不能表示精确的十进制值,因为它们表示二进制分数。 Unless you want to end up with values such as $1.0000000000001, use a data type that is integral, such as a decimal type. 除非要以$ 1.0000000000001这样的值结尾,否则请使用整数形式的数据类型,例如十进制类型。

The reason why comparison using an == operator does not work as intended is because many numbers that can be represented in decimal cannot be represented in floating point, therefore, there is no exact match. 使用==运算符进行比较无法按预期进行的原因是,由于许多可以用十进制表示的数字无法用浮点表示,因此没有精确匹配。

Here's a little example: 这是一个小例子:

System.out.println(1.00000001f == 1.00000002f);
System.out.println(200000.99f - 0.50f);

Outputs: 输出:

true
200000.48

Those two examples should show that relying on floating point values for currency would not be such a good idea. 这两个例子应该表明,依靠浮点值获取货币并不是一个好主意。

As for the storage of currency values, it appears that there is a DECIMAL type in MySQL as well, so I would think that would be a better choice, unless there is some CURRENCY type -- I'm not familiar enough with SQL databases to give any informed opinion here. 至于货币值的存储,在MySQL中似乎也有DECIMAL类型,所以我认为这是一个更好的选择,除非有某种CURRENCY类型-我对SQL数据库还不熟悉,在这里给出任何见解的意见。

Here's a little information from the MySQL 5.1 Reference Manual, Section 10.2: Numeric Types : 这是《 MySQL 5.1参考手册》的第10.2节:数字类型

The DECIMAL and NUMERIC data types are used to store exact numeric data values. DECIMALNUMERIC数据类型用于存储精确的数值数据值。 In MySQL, NUMERIC is implemented as DECIMAL . 在MySQL中, NUMERIC被实现为DECIMAL These types are used to store values for which it is important to preserve exact precision, for example with monetary data. 这些类型用于存储对于保留精确度非常重要的值,例如对于货币数据而言。

Here's a related question: 这是一个相关的问题:

When you compare floating point numbers you must always compare them within a small range, say plus or minus half a penny when dealing with currency. 比较浮点数时,必须始终在较小范围内进行比较,例如在处理货币时,请说加或减半便士。 If you compare them with exact equality, you will always get errors because floating point does not represent most decimal numbers precisely. 如果将它们与完全相等进行比较,则总是会出错,因为浮点数不能精确地表示大多数十进制数。

It is because of this problem that money is usually stored in mySql as DECIMAL rather than floating point. 由于这个问题,金钱通常以DECIMAL而不是浮点的形式存储在mySql中。 DECIMAL does not suffer from the same inaccuracies. DECIMAL不会遭受相同的误差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM