Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Padding with zeros  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My application is accepting inputs ranging from 4-6 digits long.
But if the user types in either of 4/5 digits long; I will pad it
with leading zeros. Is there any utility in the Java API that
does this for me ? Thanks.

e.g. input: 3457, output: 003457
input: 00122, output: 000122

Pho
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check java.text.DecimalFormat


[ June 20, 2005: Message edited by: Mani Ram ]
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, you need to be careful when parsing numbers with leading 0's ... They are interpreted as being octals (base 8)
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestions.

Pho
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DecimalFormat only formats numbers. If your users enter Strings you'll have to convert to number first. That's a good check that they entered a valid number.

If you think you can skip the convert & validate bit, just concatenate strings:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!