Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting integers - should be easy?!

 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm formatting some text output and want to format an int so that the resulting String is:
* the resulting String is a minimum of (say) n characters wide
* the integer is right-aligned
* the string is padded out with spaces, not '0's.
Ie, I want format(int num, int spaces) such that:
format (100, 6) => ' 100'
format (10000), 6) => ' 10000'
I thought I'd be able to do this in two lines with DecimalFormat or NumberFormat, but I can't work it out. Am I missing something blindingly obvious?!
This is the sort of code I'm playing around with, it seems like I can either have space pads and left-alignment, or '0' pads and right-alignment.

I know it'd be trivial to write a function myself to do this, but I'm wondering if the standard Java libraries make it possible...I was sure they would
Maybe I should rewrite in Perl?? ;-)

Cheers,

-Tim
BTW, the specification for patterns in DecimalFormat is here: http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh, I did it myself. Still annoyed that I had to do it manually...
Though slightly more annoyed that I spent 1/2h reading API docs for classes I didn't use, but wrote this in 2 mins:

Still, I learned a lot about formatting ints
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim:
If you are using the new J2SE 1.5 beta, then what you want to do is really easy: just use the printf() method that is defined by PrintStream. It's too long to discuss all of its features in this forum, but the J2SE 1.5 docs have a complete description. However, here is an example that uses it to do what you want. It uses a field-width specifier to right-justify a value within a specified field width. It pads with spaces.

The output is shown here:

Hope this helps.
[ March 26, 2004: Message edited by: Herb Schildt ]
 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>If you are using the new J2SE 1.5 beta, then what you want to do is really >easy:
and if you dont use it yet... then it's still quite easy, provided you know how to write loops.
The problem with your code is, that you cannot change the lenght of the field without deleting or adding spaces in your code.
I'd try something like this:
public static String nbrFormat( int lenght, int n )
{
String s = Integer.toString( n );
while ( lenght > s.length() )
{
System.out.print( " " );
lenght--;
}
return s ;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic