当我将 78.9 插入 Mysql(使用 JDBC)时,它会四舍五入为 79 吗?这是正常的吗

发布于 2024-09-12 15:57:33 字数 428 浏览 6 评论 0原文

当我将 78.9 插入 Mysql(使用 JDBC)时,它会四舍五入为 79 吗?这是正常的吗...如果是的话我怎样才能阻止这种情况发生。

更多详细信息:

列名称:num 数据类型:十进制(12,0) * 以上项目是从 phpMyAdmin 复制的

查询是

stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+",78.9)");

理想情况下我会使用变量而不是硬编码的 78.9

例如

        BigDecimal obj = new BigDecimal(78.9);

When I insert 78.9 into Mysql (using JDBC) it gets rounded up to 79? Is this normal ... if so how can I stop this from happening.

More details:

Column name: num
Data type: decimal(12,0)
* The above item was copied from phpMyAdmin

Query is

stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+",78.9)");

Ideally I would use a variable instead of the hard-coded 78.9

Such as

        BigDecimal obj = new BigDecimal(78.9);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-09-19 15:57:33

这是正常的,因为在 Decimal(12, 0) 中使用 0 本质上表示小数点后不应该有任何内容,因此它会向上舍入。您需要指定比例才能获得您要查找的内容。例如,decimal(12,5) 允许小数点左边有 7 个数字,右边有 5 个数字。

The declaration syntax for a DECIMAL column is DECIMAL(M,D). 
The ranges of values for the arguments in MySQL 5.1 are as follows:

M is the maximum number of digits (the precision). 
It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

D is the number of digits to the right of the decimal point (the scale). 
It has a range of 0 to 30 and must be no larger than M.

请参阅:http://dev.mysql。 com/doc/refman/5.1/en/ precision-math-decimal-changes.html

It's normal because using the 0 in decimal(12, 0) essentially says there should be nothing after the decimal, so it rounds up. You need to specify the scale to get what you're looking for. For example, decimal(12,5) would allow 7 numbers to the left of the decimal and five to the right.

The declaration syntax for a DECIMAL column is DECIMAL(M,D). 
The ranges of values for the arguments in MySQL 5.1 are as follows:

M is the maximum number of digits (the precision). 
It has a range of 1 to 65. (Older versions of MySQL permitted a range of 1 to 254.)

D is the number of digits to the right of the decimal point (the scale). 
It has a range of 0 to 30 and must be no larger than M.

See: http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html

永言不败 2024-09-19 15:57:33

您需要将要插入的列的数据类型设置为

float(x,y)

decimal(x,y)

其中 x 是总位数,y 是小数点总数。

e.g. float(5,2)  -> 325.46
     decimal(10,5) -> 42579.12345

You need to set the datatype of the column you are inserting into as

float(x,y)

or

decimal(x,y)

where x is the total number of digits and y is the total number of decimals.

e.g. float(5,2)  -> 325.46
     decimal(10,5) -> 42579.12345
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文