Menu
Coddy logo textTech

Formatted Output

Part of the Fundamentals section of Coddy's Java journey — lesson 41 of 73.

challenge icon

Challenge

Beginner

Modify the output so that it will always print a double value with two decimal places. To do it use the %.2f format inside printf method:

int num = 0;
System.out.printf("number: %.2f", num);

Add \n at the end of the string, this adds a new line:

System.out.printf("number: %.2f\n", num);

Try it yourself

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("Calculator App");

        Scanner scanner = new Scanner(System.in);
        double num1 = scanner.nextDouble();
        double num2 = scanner.nextDouble();

        // Perform arithmetic operations
        double sum = num1 + num2;
        double difference = num1 - num2;
        double product = num1 * num2;
        double quotient = num1 / num2;

        // Print the results
        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product: " + product);
        System.out.println("Quotient: " + quotient);
    }
}

All lessons in Fundamentals