18

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)); } 
2
  • heh, so my answer wasnt correct to remove the # sign? :P Commented Mar 31, 2010 at 19:48
  • Removing the # fixed it for me. I needed one leading 0 so I just used ("00") and it worked. Commented Nov 21, 2011 at 17:04

5 Answers 5

27

There's an arguably more elegant solution:

String.format("%03d-%03d-%04d", areaCode, exchangeCode, number) 
Sign up to request clarification or add additional context in comments.

2 Comments

+1 much more elegant and concise. Wouldn't think of any other solution.
Crap. 20 years developer and I didn't know that. [this dude feels soooo stupid...] Thanks for sharing.
18

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 

2 Comments

No offense but, String.format is the easiest way to deal with this.
None taken. String format is indeed more elegant, but the poster already had a solution using DecimalFormat and was just asking why it wasn't working.
6

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

2 Comments

That's not what he's looking for. The format is not supposed to be 2 4-digit numbers. It's telephone number format, ###-####.
meh the point wasnt to replicate his problem it was to show how to add leading zeros...this way works
4

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

Comments

0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.