0Pricing
Java Academy · Lesson

Number and Decimal Formats

Width, precision, and padding.

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);
    }
}

All lessons in this course

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