Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

postgresql & java problem with real numbers

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
GR
Hi all,
I am writing a program in java that uses jdbc to connect to a postgres DB. Everything works fine, there is only that problem that i hope you can help. I have a table
Code:
Create table test(
a real,
b int)
Then i use this table in my program to insert some values of type float. The problem is that apparently the java float(or double actually) is not compatible with the postgres real data type.
When i try to do a select from that table with a=0.2 for example i get an empty table, but if i change that to
a>0.1 and a<0.3
i get the proper result. The problem is that i cant use something like that since i want to get part of the table dynamically from within java.

Is there a way to define how postgres real and java float work alike?

thanks for any help provided.
Ilias
 
I think i figured it out. I used numeric instead of real and it seems to be working...so far.
 
Ok but you need to be aware that there are limitations with any language's ability to handle fractional values.
It is not uncommon for a fractional number to be stored imprecisely.

In my experience it is wise to avoid floats, doubles, reals or any other floating point representation if at all possible and only to use them for numbers where you have no choice.

For example, I have seen people use floats for money simply because they see a decimal point and assume that this is the only representation that works. Clearly in any programming language they should probably be using some form of integer and representing pennies/cents rather than pounds/dollars.
It is not difficult to display an integer as though it was 100 times smaller than it is.

Database engines invariably have some form of "money" type for this specific example but without knowing what you are trying to represent it's not easy to comment further.

Best of luck and hope that helps.



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top