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
  • 665
  • 1
  • 10
  • 29

1 Answers1

24

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

String.format("%03d", yournumber);
Jochem Gruter
  • 2,813
  • 5
  • 21
  • 43