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