Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Formatting a text field
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mike200015




PostPosted: 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".
Sponsor
Sponsor
Sponsor
sponsor
klopyrev




PostPosted: 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
wtd




PostPosted: Fri Apr 20, 2007 9:16 pm   Post subject: RE:Formatting a text field

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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: