• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display a integer always in two digit  RSS feed

 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to display Time that should be in universal format. for example i should display 1 as "01" but 12 should be "12". Anyone tel me
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What distinguishes a number 0-9 from a number between 10-99 and 10-99 from 100-999? Think in terms of digits!

Hint1 : Modulus operator.
Hint2 : to string


[ December 23, 2008: Message edited by: Akhilesh Trivedi ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vinoth thirunavukarasu:
Hi,
I want to display Time that should be in universal format. for example i should display 1 as "01" but 12 should be "12". Anyone tel me


Are you trying to display an integer or a time? Your subject line says integer, but your post says time. (Precision and attention to detail are very important things in programming, and should carry over to asking of questions as well. )

You'd handle each -- integer vs time -- a little differently.

If its an integer, there are a couple of ways to do it, depending on if you merely wanted it displayed that way on standard output, or store in a String that way.

Take a look at the Java tutorial which shows how to format (big hint) a number. It discusses a couple of ways to do such. You can look at the tutorial's Really Big Index to find it a little easier.

The tutorial also has information on how to format a date and or time.
[ December 23, 2008: Message edited by: Mark Vedder ]
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear vinoth,
You can use DecimalFormat to achieve this:
Look at the below code:


Compile & run it:


Hope this is suffice.
Regards.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 5.0 introduced C-like formatting with which you can do the same:
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Rob Prime, String.format doesnot satisfy his condition of having leading zeros it just gives whitespaces.


Compile & Run:

Regards.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya narayanan:
No Rob Prime, String.format doesnot satisfy his condition of having leading zeros it just gives whitespaces.


Yes it does, if you add the necessary flags to the formatting:



The above results in the output of:
01
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Correct now it is working.

Thanks mark vedder.
Regards.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I should have done it right the first time around. I only use it all the time...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!