/* FormatOutput.java CIS 160 David Klick 2015-09-02 Demonstrates how a number can be formatted for output. Often used to set the number of decimal places displayed, such as in currency values. Note: This program crashes if the user enters something unexpected. */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.text.DecimalFormat; public class FormatOutput { public static void main(String[] args) throws IOException { // set up object to read from keyboard BufferedReader kbd = new BufferedReader( new InputStreamReader(System.in)); // set up object to format number DecimalFormat twoPlaces = new DecimalFormat("######.00"); // declare variables double x; String s, outString; // get input System.out.print("Enter a floating-point number: "); s = kbd.readLine(); x = Double.parseDouble(s); // format output outString = twoPlaces.format(x); // display number entered System.out.println("The number entered was: " + outString); } } /* Sample of three runs of program: Enter a floating-point number: 4.600 The number entered was: 4.60 Enter a floating-point number: 4.676 The number entered was: 4.68 Enter a floating-point number: 5 The number entered was: 5.00 */