Print Formatted Decimal Value

This program prints a decimal value formatted to two decimal places.

Problem Statement

Write a Java program that prints a decimal value with two decimal places.

Source Code

1 public class FormattedDecimal {
2 public static void main(String[] args) {
3 double value = 123.4567;
4 System.out.printf("Formatted value: %.2f - Eduinq", value);
5 }
6 }

Program Output

Formatted value: 123.46 - Eduinq

Explanation

%.2f Formats decimal to 2 places. printf allows formatted output. Conclusion: Shows how to format decimal values in output.

Quick Links to Explore