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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!