0

I have table in Microsoft Sql server database and it is having columns of float datatype. I am trying to retrieve the values of these column type in Java ResultSet and then using those values. Below is code for the same.

            while (rsTran.next()) {
                txnList.add(rsTran.getInt(1));
                for (int i = 4; i <= col; i++) {
                    if (i != col) {
                        if (i == 9 || i == 10) {
                            System.out.println("BigDecimal Value:" + rsTran.getBigDecimal(i));
                            System.out.println("Float Value:" + rsTran.getDouble(i));
                            tranFileWriter.append(rsTran.getBigDecimal(i) != null ? (rsTran.getBigDecimal(i)+"") : "");
                        } else {
                            tranFileWriter.append(rsTran.getString(i) != null ? rsTran.getString(i) : "");
                        }
                        tranFileWriter.append('|');
                    } else if (i == col) {
                        tranFileWriter.append("MT" + rsTran.getString(i));
                    }

                }

                tranFileWriter.append(NEW_LINE_SEPARATOR);

            }

It is behaving weird when the values for those float data type column is ending with zero. The sysout statements are printing value in exponential format when values is ending with zero and it is printing fine when value is not ending with zero. for example the value 1986373620.00, 1986373620 will be printed as below.

  BigDecimal Value:1.98637362E+9  

And if the value is 1986373622 then it prints the exact value and which is fine.

 BigDecimal Value:1986373622

Any thoughts why it behaving like this only for the values ending with zeros.

Gaurav Parek
  • 317
  • 6
  • 20
  • For the detailed formatting rules in this case, refer to the javadocs for `Double.toString(double)`. (The criterion for E format is actually the magnitude of the number, not trailing zeros.) The "why" is because they designed it that way :-) – Stephen C Sep 25 '18 at 11:39
  • I understand that Stephen but the magnitude is not giving issues here even when number is large say 1986373622 it is printing it fine. But say we have a number 15168910 which is pretty less than the previous number it fails in this scenario. – Gaurav Parek Sep 25 '18 at 12:49

0 Answers0