-3

I am having trouble displaying the text in a text view. I want to convert the input numbers from EditText to 8 digits by filling Zeros.
Say if I type in EditText as 123 The output in Textview Should be 00000123 or say I type in EditText as 1234 the Output should be 00001234. How I should do it in Android Studio?

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
AadilDAR
  • 13
  • 1

3 Answers3

0

Here is how you can add zeros in front:

String yourString = String.format("%08d", yournumber);
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
0

Try this:

For zero-padding with a length of 8

String.format("%08d", yournumber);
ex:
String value= String.format("%08d", 123); 

output is 00000123

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
0

This will help.

String input = "123";
String finalString = ("00000000" + input).substring(input.length())

If you started with a number instead:

Int inputNumber = 123;
String formattedNumber = String.format("%08d", inputNumber);
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50