• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to long datatype conversion problem

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79514
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic