×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

postgresql & java problem with real numbers

postgresql & java problem with real numbers

postgresql & java problem with real numbers

(OP)
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

RE: postgresql & java problem with real numbers

(OP)
I think i figured it out. I used numeric instead of real and it seems to be working...so far.

RE: postgresql & java problem with real numbers

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close