File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String to numeric and back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to numeric and back" Watch "String to numeric and back" New topic

String to numeric and back

Rob Gee

Joined: Sep 22, 2004
Posts: 5
This should be easy - but I'm having trouble working through it.

here's the situation. I have a field which MUST be 7 characters in length, is numeric in nature, and needs to have arithmetic functions applied. So, I guess I have multiple questions:

1) is there any way to "fix" the length of a numeric data type buy padding with "0"?
2) if not, how can I take the string value (example 0000100), convert to numeric so I can add 1 (I've succeeded in this step and the result comes back "101"), then convert back to "0000101" ??

Thanks in advance !
Derek Baker
Ranch Hand

Joined: May 23, 2003
Posts: 46
I don't think you can fix the length, but you can convert it to a number, do your operation, then convert it back. To convert it back goes something like this:

I don't have the API in from of me, so that may not be precisely right, but if you look at the java.text.DecimalFormat class API, you should be able to get the rest of it.

[ September 24, 2004: Message edited by: Derek Baker ]
Rob Gee

Joined: Sep 22, 2004
Posts: 5
Thanks Derek - took a little tinkering, but it works great !!

I agree. Here's the link:
subject: String to numeric and back
It's not a secret anymore!