1

Possible Duplicate:
Left padding integers with zeros in Java

I have a problem. i am working on a android app

I have a long timestap and a string that i need to combine. The catch is that if the long is less than 8 digits, then i need to display 0's to make it look good.

Example

long time = 311;

how can i fill it out so it will be

00000311

long time2 = 3111

00003111

I hope any of you an help me with it since i been trying to find a solution for it for over an hour now..

Rasmus

Community
  • 1
  • 1

3 Answers3

3
String.format("%08d", long time /*YourNumber*/);

%08d : zero-padding with length=8.

AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
1

You can use java.util.Formatter to pad with leading zeroes. You need to specify width and if the width of the number is less than the given width, it will be padded with leading zeroes.

http://developer.android.com/reference/java/util/Formatter.html

naresh
  • 2,113
  • 20
  • 32
0

Try NumberFormat. Like this

NumberFormat formatter = new DecimalFormat("00000000");
String s = formatter.format(time);
Rahmathullah M
  • 2,676
  • 2
  • 27
  • 44