1

In a table with number datatype I have stored 0.4

    String colLabel = rsMetadata.getColumnLabel(i);                         
    recordMap.put(colLabel.toLowerCase(), rs.getString(colLabel));

I use rs.getString to fetch the data.

But in java what I get is 4.00000000000000022204460492503130808473E-01

How can I go about this. This is a generic code where everytime rs.getString is used

5
  • 1
    One possible way: more info here, basically you determinate data type and then use getString or getBigDecimal or the related method. Commented Apr 14, 2020 at 19:15
  • column is number and it is just select column_name Commented Apr 14, 2020 at 19:16
  • 1
    @TomJava - Any update? Did any of the answers work for you? Commented Apr 16, 2020 at 11:17
  • Thank you Aravind. I followed a similar approach Commented Apr 17, 2020 at 6:01
  • @TomJava - You are most welcome. Wish you success! Commented Apr 17, 2020 at 7:29

2 Answers 2

2

The getter method of the appropriate type retrieves the value in each column. So you have to use resultSet's rs.getFloat() and that is what recommended. Getting all values with getString can be very useful, but it also has its limitations. In your case it is used to retrieve a numeric type, getString converts the numeric value to a Java String object and cannot be guaranteed that it will be the string representation of the value in the column.

Sign up to request clarification or add additional context in comments.

Comments

1

A workaround is

String colLabel = rsMetadata.getColumnLabel(i);                         
recordMap.put(colLabel.toLowerCase(), String.valueOf(Double.parseDouble(rs.getString(colLabel))));

Demo:

public class Main {
    public static void main(String[] args) {
        System.out.println(String.valueOf(Double.parseDouble("4.00000000000000022204460492503130808473E-01")));
    }
}

Output:

0.4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.