-2

I would like to generate a number between 0 and 9999 for an android application , that I would increment each time. The generated string must be fixed size. For example, for 0, I would like to have '0000' for 1, I would like to have '0001' etc.

Any ideas would be welcome. thanks

saloua
  • 329
  • 1
  • 4
  • 12

1 Answers1

1

Using StringUtils leftpad

// StringUtils.leftPad("your number", "length", 'padding character');
StringUtils.leftPad("0", 4, '0');

The lib http://www.java2s.com/Code/Jar/c/Downloadcommonlang3jar.htm or https://commons.apache.org/proper/commons-lang/download_lang.cgi

arun
  • 1,728
  • 15
  • 15
  • 1
    It would have been nice to include in the answer the fact that it requires an external library... Like in the answers in the linked question. – 2Dee Aug 28 '15 at 08:10