Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / February 2004

Tip: Looking for answers? Try searching our database.

problem with SQL Server money type and java double

Thread view: 
Andrew Park - 05 Feb 2004 19:48 GMT
we have a java application that inserts a value (1.4 as a double) into a
SQL Server table column (via an sp) that is of type money.  no problem
there.  another part of the application retreives that information (via
an sp) as a double.  the resultant number is (1.39).

what is going on?  this problem seems to occur randomly for numbers that
  are not whole or halves of a number (1.0, 1.5, 2.0, 2.5, 3.0 etc).

after retreiving the number from the database, i do format it with:

    (long)(rs.getDouble("amount") * 100);

but i don't think this has any affect on what i see.

thank you for any and all assistance.

andrew
Joe Weinstein - 05 Feb 2004 20:15 GMT
> we have a java application that inserts a value (1.4 as a double) into a
> SQL Server table column (via an sp) that is of type money.  no problem
[quoted text clipped - 13 lines]
>
> andrew

Hi. I am going to recommend something that sounds like more work, but you should
never use doubles for money. Use BigDecimal, so no precision is ever lost.
Joe Weinstein at BEA
Rick Genter - 07 Feb 2004 14:26 GMT
Doubles do not represent all values exactly. Internally, 1.4 is stored as
1.39999999999999 (1.4 does not have an exact representation in double
precision format).

I've never used the MONEY type in SQL Server, but I would either use it
consistently throughout your application, or assume you have to round when
you convert. I presume there is a rounding function you can invoke;
alternatively, you can do something like

CONVERT(MONEY, Amount * 100.0 + 0.5) / CONVERT(MONEY, 100.0)

(This is off the top of my head - you'll need to verify syntax.)
Signature

Rick Genter
<mailto:rgenter@rcn.com>

> we have a java application that inserts a value (1.4 as a double) into a
> SQL Server table column (via an sp) that is of type money.  no problem
[quoted text clipped - 13 lines]
>
> andrew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.