• 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 conversion

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can someone give me an idea to get around this small problem??
I don't want to lose "0" when I convert a string conatining "0" at the begining to lose.
For Ex:
When I convert "06789" to an int,we know that we get 6789.
Can I get 06789.
Thanx
Rajani
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you trying to parse it into an int? If you need the leading zero(s), can't you keep it as a string? Can you explain a little more about how you are using the value?
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK!!!
Let me give more insight.....
I am reading a column from a CSV file which has numbers say "06789".
I am converting all those numbers into Int and storing them into a vector.
This vector I am passing into a TreeSet for sorting,from where I am taking min and max values.
I am outputing those values to users but while doing that I want to output 06789 as 06789 instead of 6789.
HOpe I was sucessful!!!
Thanx
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.text.DecimalFormat
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking of DecimalFormat too. It works when you have a known number of digit. What if you enter "006879"? You have to keep track of the String length...
W.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then maybe ya just shouldn't be convertin' the input to a number and keep it as a String since that would seem to be what is desired.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
Then maybe ya just shouldn't be convertin' the input to a number and keep it as a String since that would seem to be what is desired.


And additionally use a custom Comparator for the TreeSet, so that the values get sorted by int value instead of lexicographically.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic