Computer Science Canada

Formatting a text field

Author:  mike200015 [ Fri Apr 20, 2007 6:53 pm ]
Post subject:  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".

Author:  klopyrev [ Fri Apr 20, 2007 7:54 pm ]
Post subject:  Re: Formatting a text field

code:

DecimalFormat dc = new DecimalFormat("00");
System.out.println("5" + ":" + dc.format(2));


Don't forget to import java.text.DecimalFormat!

KL

Author:  wtd [ Fri Apr 20, 2007 9:16 pm ]
Post subject:  RE:Formatting a text field

code:
System.out.printf("5:%02d", 2);

Author:  Hikaru79 [ Fri Apr 20, 2007 10:14 pm ]
Post subject:  Re: RE:Formatting a text field

wtd @ Fri Apr 20, 2007 10:16 pm wrote:
code:
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.


: