This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String to long datatype conversion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to long datatype conversion problem" Watch "String to long datatype conversion problem" New topic
Author

String to long datatype conversion problem

Simpson Kumar
Ranch Hand

Joined: Mar 19, 2008
Posts: 260
I have a String value which has 0 s prior to the value, I need to parse that string long primitive type. When I do that, I got the values without 0 s, but I need them too.

How to get them too

String s = "000234";
long l = Long.parseLong(s);

the output for the top is displaying as 234 only, but I need 000234 too. how?


Thanks,
Kumar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
A number is recorded as a number, whose value is independent of leading 0s. (That doesn't apply to literals however.) If you need to record the 000 at the beginning, it is easiest to maintain it as a String. I don't think BigInteger will help you.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Why do you need to retail all the preceding zeros? I will say go for some other way to retain the leading zeros. Once you convert it into number then preceding zeros have no worth and hence they are removed.


My Blog SCJP 5 SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to long datatype conversion problem
 
Similar Threads
Pagination for records coming from Web Services
string to byte array problem
null comparison
Binary to Decimal conversion
fastest equality check: 2 strings, or 2 byte[]?