简体   繁体   English

Java ResultSet加零

[英]Java ResultSet adding zeros

Weird behavior here. 这里的行为很奇怪。

On the SQL Server database the columns type are Numeric(20,6) but the resultset retrieves the values added by a bunch of zeros: 在SQL Server数据库上,列类型为Numeric(20,6),但结果集检索由一堆零添加的值:

Table: 952.346,44 - 890.00 表格:952.346,44-890.00

Resultset: 952346.440000 -> 890000.000000 结果集:952346.440000-> 890000.000000

The application is running on WAS 8.5, Java 7 with JDBC Driver sqljdbc4-4.1.jar 该应用程序在带有JDBC驱动程序sqljdbc4-4.1.jar的WAS 8.5,Java 7上运行

As you have mentioned Numeric(20,6) which means the scale of the number is 6 正如您提到的Numeric(20,6) ,这意味着Numeric(20,6)的小数位数是6

And thats the reason the same precision is considered in Java. 这就是在Java中考虑相同精度的原因。

Now if you want to reduce it 2 decimal places, you can simply update column type or if it is already in production you can simply reduce the number in JAVA with various possible ways, on of which is as follows: 现在,如果要将其减少2个小数位,则可以简单地更新列类型,或者如果它已经在生产中,则可以使用各种可能的方法简单地减少JAVA中的数目,其方法如下:

Double.parseDouble(String.format("%.2f", 952346.440000));

Your column is a NUMERIC(20,6) , the 6 there is the scale, and that specifies the number of decimals. 您的列是NUMERIC(20,6) ,其中6是小数位,它指定小数位数。 In other words, the values already were 952346.440000 and 890000.000000 , and JDBC just returned them according to specification. 换句话说,这些值已经 952346.440000890000.000000 ,而JDBC只是根据规范返回了它们。

If you need only 2 decimals, then use NUMERIC(20,2) . 如果只需要2个小数,则使用NUMERIC(20,2) Or rescale the number in your code using one of the BigDecimal.setScale methods. 或使用BigDecimal.setScale方法之一重新缩放代码中的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM