Using Java String Formatting printf, format

Note that, System.out.printf and System.out.format is exactly the same, the Java engineer come out with System.out.printf may be want to make C Programmer happy.

Now, I am going to use this System.out.printf to format my money, in account book, for example, there are different type of formatting. I now tied up with my code, and the output let you to see what happen in this.

import java.util.*;
import java.text.*;

class MoneyForm {
    private double amount;
    private int type;
    public MoneyForm(double amount, int type) {
        this.amount = amount;
        this.type = type;

    public void generateForm() {
        switch(type) {
            case 0:
            System.out.printf(" >$%1$-+,(15.3f< ", amount);
            case 1:
            System.out.printf(" >$%1$0+,(15.3f< ", amount);
            case 2:
            System.out.printf(" >$%1$0+(15.3f< ", amount);
            case 3:
            System.out.printf(" >$%1$+(15.3f< ", amount);
            case 4:
            System.out.printf(" >$%1$+,(15.3f< ", amount);


class Money {
    public static void main(String args[]) {
        for(int i=0; i<5; i++)

    public static void printMoney(int type) {
        System.out.println("Money Form of type: " + type);
        for(int i=-3; i<=3; i++)
            new MoneyForm(i * 1530.21, type).generateForm();


System.out.printf Java Formatting

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.