user69514
user69514

Reputation: 27629

Java add leading zeros to a number

I need to return a string in the form xxx-xxxx where xxx is a number and xxxx is another number, however when i have leading zeros they disappear. I'm trying number formatter, but it's not working.

 public String toString(){
        NumberFormat nf3 = new DecimalFormat("#000");
        NumberFormat nf4 = new DecimalFormat("#0000");
        if( areaCode != 0)
            return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
        else
            return exchangeCode + "-" + number;
    }

}

I figured it out:

 public String toString(){
        NumberFormat nf3 = new DecimalFormat("000");
        NumberFormat nf4 = new DecimalFormat("0000");
        if( areaCode != 0)
            //myFormat.format(new Integer(someValue));
            return nf3.format(new Integer(areaCode)) + "-" + nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
        else
            return nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
    }

Upvotes: 18

Views: 36879

Answers (5)

imansdn
imansdn

Reputation: 1257

you can use NumberFormat class and set the minimum zeros as 0 easliy and it works like a charm for example for currency format:

   formatter = NumberFormat.getCurrencyInstance();
   formatter.setMinimumFractionDigits(0);
   -----------
   print ( formatter.format(119.00) ) ---> $119

Upvotes: 0

Kuchi
Kuchi

Reputation: 4674

I would recommend using the NumberFormat (http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html)

In my opinion it gives the best readability. And also minimizes the possibility of errors when you put a wrong String into the DecimalFormat.

  final int value1 = 1; 
  final double value2 = 4.2;

  NumberFormat nf = NumberFormat.getInstance(Locale.US);
  nf.setMinimumIntegerDigits(2);

  System.out.println(nf.format(value1));
  System.out.println(nf.format(value2));

Output:

  01
  04.2

(The Locale is optional but I recommend it when you work with an international team. Default value are the local settings)

Anyway NumberFormat in this way is such a great thing, especially if you have to deal with different countries or things like percentage or currency

Upvotes: 4

Tomislav Nakic-Alfirevic
Tomislav Nakic-Alfirevic

Reputation: 10173

There's an arguably more elegant solution:

String.format("%03d-%03d-%04d", areaCode, exchangeCode, number)

Upvotes: 27

Justin Ardini
Justin Ardini

Reputation: 9866

When areaCode is 0, you forget to call format! Other than that, it looks fine. The leading "#" are not necessary, but won't cause any problems for valid inputs.

I just tried it out real quick to check and it worked fine for me.

public static String formatTest(int areaCode, int exchangeCode, int number) {
    DecimalFormat nf3 = new DecimalFormat("#000");
    DecimalFormat nf4 = new DecimalFormat("#0000");
    if( areaCode != 0)
        return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
    else
        return nf3.format(exchangeCode) + "-" + nf4.format(number);
}


public static void main(String[] args) {
    System.out.println(formatTest(12, 90, 8));
    System.out.println(formatTest(1, 953, 1932));
}

Output:

012-090-0008
001-953-1932

Upvotes: 18

Holograham
Holograham

Reputation: 1368

Remove the # sign

http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

This code:

import java.text.DecimalFormat;
import java.text.NumberFormat;


public class Test
{

    public static void main(String[] args) 
    {       
        int areaCode = 123;
        int exchangeCode = 456;

        NumberFormat nf3 = new DecimalFormat("0000");

        System.out.println(nf3.format(areaCode) + "-" + nf3.format(exchangeCode) );
    }

}

Produces this output:

0123-0456

Upvotes: 6

Related Questions