Formatting a double value within the toString() method (with respect to the decimal)

This is a fairly simple question. I have a class of objects Item which constructs the objects declared in the TestItem class. The overridden toString() method utilises the DecimalFormat class in order to format my double value price. When my toString() method is written as such, my output is produced in this fashion:

item output

The only modification in formatting I desire is alignment with respect to the decimal, how do I achieve this?

     public String toString() {
        DecimalFormat format = new DecimalFormat("##.00");
        String add = "";
        if(itemName.length() < 7) {
            add = "\t";
        }
        return itemID + " \t " + itemName + add + "\t" + inStore  + "\t   $" + format.format(price);
    }

public class TestItem {
    public static void main(String [] args) {
        Item list[] = { new Item(1011, "Air Filters", 200, 10.5),
                        new Item(1034, "Door Knobs", 60, 21.5),
                        new Item(1101, "Hammers", 90, 9.99),
                        new Item(1600, "Levels", 60, 19.99),
                        new Item(1500, "Ceiling Fans", 100, 59),
                        new Item(1201, "Wrench Sets", 55, 80) };
        printMovies(list);
    }

    public static void printMovies(Item list[]) {
        System.out.println("itemID\t itemName\tinStore\t    price");
        System.out.println("------------------------------------------");
        for(Item token : list) {
            System.out.println(token);
        }
        System.out.println();
    }
}

This isn't the full code; only what is relevant.

Jon Skeet
people
quotationmark

You can use String.format to control the format in general. Unfortunately I can't see any way of combining that with a custom pattern directly, but you can compose them. For example:

import java.math.*;
import java.text.*;

public class Test {
    public static void main(String[] args) {
        showPrice(new BigDecimal("123.456"));
        showPrice(new BigDecimal("11.12"));
        showPrice(new BigDecimal("10.5"));
        showPrice(new BigDecimal("1.5"));
        showPrice(new BigDecimal("0.5"));
    }

    static void showPrice(BigDecimal price) {
        DecimalFormat format = new DecimalFormat("0.00");
        String text = String.format("Price: $%5s", format.format(price));
        System.out.println(text);
    }
}

Output:

Price: $123.46
Price: $11.12
Price: $10.50
Price: $ 1.50
Price: $ 0.50

Note that:

  • The $ signs are still aligned (the space comes after that)
  • The "wide" price doesn't look good, but there's not much you can do about that
  • Using "0.00" instead of "##.## means the last price is "0.50" rather than ".5"

You can use String.format to format all of your data, controlling the width of fields with the format specifier. You might want to use spaces instead of tabs to make it easier to ensure it's aligned.

people

See more on this question at Stackoverflow