
-----------------------------------
mike200015
Fri Apr 20, 2007 6:53 pm

Formatting a text field
-----------------------------------
I was wondering how to format a text field to display a 0 infront of numbers less than 10. I am making a digital clock, and need to display the 0 in front, for example "5:02".

-----------------------------------
klopyrev
Fri Apr 20, 2007 7:54 pm

Re: Formatting a text field
-----------------------------------

DecimalFormat dc = new DecimalFormat("00");
System.out.println("5" + ":" + dc.format(2));


Don't forget to import java.text.DecimalFormat!

KL

-----------------------------------
wtd
Fri Apr 20, 2007 9:16 pm

RE:Formatting a text field
-----------------------------------
System.out.printf("5:%02d", 2);

-----------------------------------
Hikaru79
Fri Apr 20, 2007 10:14 pm

Re: RE:Formatting a text field
-----------------------------------
System.out.printf("5:%02d", 2);

As a quick note, I believe wtd's use of printf will only work on Java 5.0 or later; if you're using what your school gave you (most likely RTP with the Jikes compiler that implements Java 1.4.2), it won't work. Just to pre-empt any confusion.
