File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Converting a string to a double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting a string to a double" Watch "Converting a string to a double" New topic
Author

Converting a string to a double

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I'm sure this is rather simple to do , but I'm having one of those days

I am bringing back a value from our DB (DB2) which is of the string type.
The value happens to be 001000 when it comes back to me.
I have converted this value to a double which makes it 1000.0;
This works, however, I now need to take this value and go out to another table in the db and compare it. The column in the table I want to compare it to is a numeric field with 3 decimal positions. (Numeric(6,3));
Obviously when I compare the data, it's not correct. I need the value I am comparing to the DB to read 1.000, and not 1000.0;
How can I get my double value of 1000.0 converted to 1.000 ???
I tried DecimalFormat with ("0.000") but that gives me 1000.000.
What am I missing here?
Thanks for any help!
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Here's a simple suggestion - divide by 1000.
value /= 1000;
Joe
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I could do that, however it won't work for all of the other values
I'm going to be reading from the DB.
For instance, some of the values parse to double OK, like the value
032968 in the DB as a string converts OK when I parse it to a double
(gives me 32.968).
If I divide that by 1000, the value will be incorrect (.032968).
Any other suggestions?
Thanks!!
[ September 15, 2003: Message edited by: Jennifer Sohl ]
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Okay, now I see the issue. You're saying that sometimes the decimal is used, sometimes it isn't.
This doesn't make sense.
If 001000 parses to 1000.0, then 032968 should parse to 32968.0.
Let's see your parsing code.
Joe
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the reply! I think I've straightened myself out.
Turns out the 032968 was actually a value I was using in my program from a different column that was defined as Numeric(6,3). That's why it was formatted correctly!
After looking at my requirements, it will work to just divide all of my values by 1000 and get the decimal where I want it.
Guess I just needed a break from this stuff to see the light...
Thanks for your help!
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Often all you need is another set of eyes, Jennifer.
Glad I could help.
Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting a string to a double