Question
4.7 Lab Warmup: Numeric Output Formatting The template code reads in two numbers from the user. Modify the code to: (1) Use printf to output
4.7 Lab Warmup: Numeric Output Formatting
The template code reads in two numbers from the user. Modify the code to:
(1) Use printf to output the numbers rounded to 1 decimal place, so that their decimals line up. Display leading zeros, with a total of 6 digits displayed before the decimal. (2) After a blank line, use printf to output the numbers a second time, rounded to 2 decimal places, so that their decimals line up. Display blanks before the actual digits, with a total of 6 characters appearing before the decimal.
Enter two double numbers: 4.567 876.5432 000004.6 000876.5 4.57 876.54
import java.util.Scanner;
public class NumberFormatting { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double num1 = 0.0; double num2 = 0.0; System.out.println("Enter two double numbers:"); num1 = keyboard.nextDouble(); num2 = keyboard.nextDouble(); System.out.println(); // FIXME (1): Output to 1 decimal place with leading zeros (6 digits before decimal) System.out.println(); // FIXME (2): Output to 2 decimal places with leading spaces (6 characters before decimal) return; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started