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
The %.2f format specifier rounds and formats the decimal value to two places, and printf is used to display formatted output, showing how to format decimal values in output.