الاثنين، 1 يونيو، 2015

Converting number to three digit representation eg (1 = 001) in JAVA

Hello everyone
Here is a simple code to converting number to three digit representation by adding prefix to number e.g (1 = 001)  using in java programing language:

First Method:

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;
}


Second Method:

String.format("%03d", yournumber);

Thank you....

Happy programming :)

ليست هناك تعليقات:

إرسال تعليق