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
- printf Basics
- Number and Decimal Formats
- String.format
- Aligning Tables