gpt4 book ai didi

java - mysql 中的 float 和 double 数据类型不存储我的 java 程序所需的完整数字

转载 作者:行者123 更新时间:2023-11-30 21:29:44 25 4
gpt4 key购买 nike

我正在使用我的 java swing 应用程序将一些值存储在我的 mysql 数据库中,但它不会存储输入数字的全部数量。例如,如果我使用 float 作为 mysql 数据库列的数据类型,它只存储 6 位数字,如果我输入 123456.56,它只会存储 123456.00,或者如果我输入 12345.67,它只会存储 12345.60。同样,当我将 mysql 的数据类型更改为 double 时,它​​只存储 8 位数字,所以如果我输入 1234567.89,它只会存储 1234567.90,并且会舍入第 8 位数字。我的编码有什么问题?谢谢。我想将全部金额存储在数据库中,例如数百万和数十万,例如。完整的 12345678.55 或 445874.44 没有将其变成指数量或将其缩短。我该怎么做?

if(source == btn_confirm)
{
dbconnect connect = new dbconnect();

String page = text_page.getText();
String line = text_line.getText();
String refpage = text_refpage.getText();
String refline = text_refline.getText();
String transaction_date = text_transdate.getText();
String description = text_desc.getText();
String account_code = text_accountcode.getText();
String code_type = text_codetype.getText();
String issue_date = text_issuedate.getText();
String amount = text_amount.getText();

System.out.println(amount);

try
{
connect.addTransaction(page, line, refpage, refline, transaction_date, description, account_code, code_type, issue_date, amount);
connect.close();
}
catch (Exception x)
{
System.out.println(x);
}

text_page.setText("");
text_line.setText("");
text_refpage.setText("");
text_refline.setText("");
text_transdate.setText("");
text_desc.setText("");
text_accountcode.setText("");
text_codetype.setText("");
text_issuedate.setText("");
text_amount.setText("");

text_page.requestFocus();
}

text_amount 是包含要传递给字符串金额的数字的文本字段。 string 曾经是一个 float ,但我选择使用 string 代替,因为我认为将它传递到 sql 语句时不会有太大区别。我以前使用 parsefloat。

addTransaction 方法将事务添加到 mysql 数据库中:

 public void addTransaction(String page, String line, String refpage, String refline, String transaction_date, String description, String account_code, String code_type, String issue_date, String amount) throws Exception
{
try
{
connect();
statement = conn.createStatement();
statement.executeUpdate("insert into transaction (page, line, refpage, refline, transaction_date, description, account_code, code_type, issue_date, amount) values ('" + page + "', '" + line + "', '" + refpage + "', '" + refline + "', '" + transaction_date + "', '" + description + "', '" + account_code + "', '" + code_type + "','" + issue_date + "', " + amount + ")");
System.out.println("Inserted transaction of page: " + page + " and line: " + line);
}
catch (Exception e)
{
throw e;
}
}

最佳答案

FLOAT 只有 24 位精度。这相当于大约 7 位小数。因此,不可能存储比您所看到的更多的内容。

FLOAT(12,2) 我想不出有什么用。它四舍五入——一次是十进制,一次是二进制。使用 DECIMAL(12,2) 或普通 FLOATDOUBLE

DOUBLE 为您提供大约 16 位的精度。同样,不要将 (m,n) 与它一起使用,也不要指望它可以在 53 中容纳更多的数字。

MySQL 可以处理 65 位十进制数字:DECIMAL(65, ...)。但是,它是固定的,因为您在小数点后指定了常数位数。

关于java - mysql 中的 float 和 double 数据类型不存储我的 java 程序所需的完整数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56880240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com