• 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

Help with int Conversion to String

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an int that i would want to convert to String if the integer is less than 4 digit it should append zeros to fill in the missing digits for example

int input=9
the function convertToString(input) should return 0009

int input=19
the function convertToString(input) should return 0019


int input=119
the function convertToString(input) should return 0119


int input=1119
the function convertToString(input) should return 1119
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you getting stuck?

This may not be the most efficient psuedo-code, but here's an approach

[code]
change int to String
add "000" in front of string
get a substring of string.length-4 - string.length
[code]

In the two minutes of thinking about it, this is the simplest approach I could come up with. There are several other ways - looping, and conditionals come to mind, but I think we'd all like to avoid control structures when possible.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try something like the following:



It's certainly not optimal given the string concatenation and there's probably some parameter checking you'd want to add but I believe it does what you're looking for.

Dave
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks i was able to do it using

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another solution:

And one more, for if you're using a Java version older than 5.0:

[ November 14, 2007: Message edited by: Jesper Young ]
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper's solution is the one I was looking for - it's definately the best way to do it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic