当我将 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);
最佳答案
这是正常的,因为在 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
关于java - 当我将 78.9 插入 Mysql(使用 JDBC)时,它会四舍五入为 79 吗?这是正常的吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3406583/