Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL getFloat question

 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48647
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48647
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic