wood burning stoves 2.0*
The moose likes Java in General and the fly likes SQL getFloat question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "SQL getFloat question" Watch "SQL getFloat question" New topic
Author

SQL getFloat question

Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Hi,

I am retrieving the numeric value from the resultset using getFloat.

but the value is getting rounded automatically above a certain limit.
1234.91 was not rounded but 9123441.91 was rounded to 9613042.0.
What is the reason of this? I know that float can hold a lot larger value.

When I tried rs.getString(i) i got 9123441.91 as expected.

Can I safely get the numeric value using rs.getString(i) instead of getFloat?

Please advise.

Thanks and regards,
Karan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

I doubt the problem is in the float itself, just in how it is printed.

What happens if you print it as follows:
System.out.printf("%.2f", rs.getFloat(i)) ?

That forces two digits to be printed instead of only 1.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Originally posted by Karan Jain:
1234.91 was not rounded but 9123441.91 was rounded to 9613042.0.
That's quite a lot of rounding from 912. . . to 961!

But joking aside, you realise that a float is only accurate to 6-7 decimal significant figures? 9123441.91 is 9 digits, and you can't expect a float to hold 9 digits.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
More about this: I thought that I was getting confused between the SQL float type and the Java primitive float type. The ResultSet#getFloat() methods return a float, the Java primitive type, so that has only 6-7 sig fig. The SQL float type appears to be arbitrary precision, so you might be better seeing if you can get it into a BigDecimal.
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Thanks for the reply guys!

I think I cannot use getFloat then as I will loose the precision.

I am currently using getString and it is working fine.
Do you see any potential problem in that?

Thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQL getFloat question
 
Similar Threads
reading excel file using jdbc [help me]
reading an Excel file from java application
connection problem
why i can't get the whole string when rs.getString() called ?
SQL Server and real data type