This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Decimal Alignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Decimal Alignment" Watch "Decimal Alignment" New topic

Decimal Alignment

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
Dear all,
I have the following numbers

1.2 5.6
62.4 87.2

how do i align to become

1.2 5.6
62.4 87.2

soo tt the decimal symbols are aligned?
Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
I think my additional spaces get removed

should be like this

#1.2 #5.6
62.4 87.2

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Use java.text.DecimalFormat to format them. In Java 5, you could also use the new Formatter capability; see System.out.printf().

[Jess in Action][AskingGoodQuestions]
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Using the # will indeed leave out 0.

The format you want to use is something like this #0.00

Look at the explanation of the pattern in the javadoc

DecimalFormat javadoc
I agree. Here's the link:
subject: Decimal Alignment
Similar Threads
Sorting java.lang.objects
Formatting strings as money format
Is there a jquery equivalent to python’s “sum” built-in function?
are there anybody know how to use WSDD to run a pocket PC emulator
Math.round() question