Ank*_*kur 2 java mysql decimal bigdecimal
当我将78.9插入Mysql(使用JDBC)时,它会被舍入到79?这是正常的......如果是这样的话我怎么能阻止这种情况发生.
更多细节:
列名:num
数据类型:decimal(12,0)
*以上项目是从phpMyAdmin复制的
查询是
stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+",78.9)");
Run Code Online (Sandbox Code Playgroud)
理想情况下,我会使用变量而不是硬编码的78.9
如
BigDecimal obj = new BigDecimal(78.9);
Run Code Online (Sandbox Code Playgroud)
这是正常的,因为使用十进制(0,0)中的0基本上表示小数点后应该没有任何内容,因此它会向上舍入.您需要指定比例以获得您想要的内容.例如,十进制(12,5)将允许小数点左边的7个数字和右边的五个数字.
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