0Pricing
Java Academy · Lesson

Number and Decimal Formats

Width, precision, and padding.

Number and Decimal Formats is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Formatting Numbers

Numbers often need special treatment: a fixed number of decimals, padding, or a sign. printf specifiers give you that control.

This lesson focuses on %d for integers and %f for floating-point values.

The %f Specifier

%f formats a floating-point number such as a double or float.

By default %f shows six digits after the decimal point, which is usually more than you want.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%f\n", 3.14);
    }
}

Controlling Decimals with .2

Add a precision like .2 between the % and the f to limit decimals. %.2f means "two digits after the point".

The value is rounded, not just cut off.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%.2f\n", 3.14159);
    }
}

Rounding with printf

Precision rounds to the nearest value. %.0f shows no decimals at all and rounds to a whole number.

Note this only changes how the number is displayed, not the stored value.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%.0f\n", 2.7);
    }
}

Money Example

Currency almost always uses two decimals. %.2f is perfect for prices.

Combine it with text to build a clean line.

public class Main {
    public static void main(String[] args) {
        double price = 9.5;
        System.out.printf("Total: $%.2f\n", price);
    }
}

Minimum Width

A number between % and the letter sets a minimum width. %5d prints the integer in a field at least 5 characters wide, padded with spaces on the left.

This is the first step toward aligned columns.

public class Main {
    public static void main(String[] args) {
        System.out.printf("[%5d]\n", 42);
    }
}

Width with Decimals

Width and precision combine: %8.2f means "at least 8 characters wide, with 2 decimals".

The width counts the digits, the decimal point, and any padding spaces.

public class Main {
    public static void main(String[] args) {
        System.out.printf("[%8.2f]\n", 3.5);
    }
}

Zero Padding

Put a 0 right after % to pad with zeros instead of spaces. %05d fills the left side with zeros.

Great for fixed-length IDs or clock digits like 09.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%05d\n", 42);
    }
}

Thousands Separator

A comma flag %,d groups digits with separators, making big numbers readable.

It also works with %f, for example %,.2f.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%,d\n", 1000000);
    }
}

Showing the Plus Sign

The + flag forces a leading sign even for positive numbers. %+d prints +5 instead of 5.

Useful for showing gains and losses clearly.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%+d and %+d\n", 5, -3);
    }
}

Hexadecimal with %x

Beyond decimals, %x prints an integer in hexadecimal and %o in octal.

You will use %d and %f most, but it is good to know other number bases exist.

public class Main {
    public static void main(String[] args) {
        System.out.printf("%x\n", 255);
    }
}

Quick Check

Pick the right specifier.

Recap

You can now control decimals with %.2f, set widths like %5d, pad with zeros via %05d, group with %,d, and force signs with %+d.

Next you will capture formatted text into a String.

Frequently asked questions

Is the “Number and Decimal Formats” lesson free?

Yes — the full text of “Number and Decimal Formats” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Number and Decimal Formats”?

Width, precision, and padding. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Number and Decimal Formats” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. printf Basics
  2. Number and Decimal Formats
  3. String.format
  4. Aligning Tables
← Back to Java Academy