It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Formatting integers - should be easy?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Formatting integers - should be easy?!" Watch "Formatting integers - should be easy?!" New topic
Author

Formatting integers - should be easy?!

Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
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

Joined: Mar 15, 2004
Posts: 539
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

Joined: Oct 01, 2003
Posts: 239
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 ]

For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
>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 ;
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Formatting integers - should be easy?!