1

Possible Duplicate:
Add leading zeroes to number in Java?

I want to add prefix to my number. I have number which i want to convert it in 3 digits e.g if i pass 1 than it should return 001. Here is my code.

public int returnThreeDigitNo(int number)
{
    int threeDigitNo = 0;
    int length = String.valueOf(number).length();
    if(length == 1)
    {
        threeDigitNo = 00+number;
    }
    if(length == 2)
    {
        threeDigitNo = 0+number;
    }
    if(length == 3)
    {
        threeDigitNo = number;
    }
    return threeDigitNo;
}

thanks in advance

Community
  • 1
  • 1
Antarix
  • 655
  • 1
  • 9
  • 28

1 Answers1

23

If you want to display an int with 3 digits you can use:

String.format("%03d", yournumber);
Jochem Gruter
  • 2,645
  • 4
  • 20
  • 37