This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes displaying decimal numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "displaying decimal numbers" Watch "displaying decimal numbers" New topic
Author

displaying decimal numbers

H.A.N. Schultz
Greenhorn

Joined: Oct 05, 2003
Posts: 1
I need to show decimal numbers in columns with the decimal points
aligned. I can format the numbers to 2 decimal places but using
tabs to align the columns they are always left justified and I
need to show them with right justification. I thought FieldPosition
would do it but I can't figure out how to use it.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I don't know if there's anything in the API, but try this (for fonts like courier)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you can get by with a little less arithmetic:

If you want to generalize this to a reusable routine, you can't always have a pad string of the right number of blanks. This might be worth while:

I left out some details ... what if aPadWith is more than one character long? Would that be useful? Could your string wind up too long? Could we get more efficient than concatenating strings in a loop? I leave these questions to entertain you late into the coding night
[ October 06, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: displaying decimal numbers