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

标签 java mysql decimal bigdecimal

当我将 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/

相关文章:

java - 嵌套 ScrollView 不滚动到 Activity 底部

java - 为什么我的 2D 迷宫没有用 JComponent 绘制?

php - 如何为简单的多语言网站(PHP MySQL)构建数据库?

java - @RequestBody 是否应该在 Spring Webflux 中用 Mono Publisher 包装?

java - ClassNotFoundException 公共(public)软件 CameraFragment

mysql - PyMySQL==0.7.11,MySQL服务器消失了

mysql - 如何作为 (Windows) 客户端连接到 mySQL 数据库?

c# - 使用十六进制而不是十进制更有效率吗?

c# - 指数 0 值的十进制解析 (0+E3)

python - 为什么django使用逗号作为小数点分隔符