11 chapter 3 decision structures cont’d. 22 formatting floating-point values with the...

31
1 Chapter 3 DECISION STRUCTURES CONT’D

Upload: annabella-gilmore

Post on 26-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

11

Chapter 3DECISION STRUCTURES CONT’D

22

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

• We can use the DecimalFormat class to control the way floating-point numbers are displayed.

• The DecimalFormat class is part of the Java API, but it is not available to your program unless you include the following import statement before the class definitions in your source file.

import java.text.DecimalFormat;

33

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

• To use the DecimalFormat class we create an object or instance of this class in memory using the new operator.

DecimalFormat formatter = new DecimalFormat("0.0#");

44

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

DecimalFormat formatter = new DecimalFormat("0.0#");

• In the statement above, we are declaring a variable named formatter that is of type DecimalFormat. Remember, a variable of a class type is a reference variable. It can reference an object (hold the address of an object) of the specified class.

• We are using the new operator to instantiate an object of the DecimalFormat class and also assigning the address of this object to the variable formatter.

• When an object is created, a special method called a constructor is executed.

• We are passing the string “0.0#” as an argument to this method. This string is a formatting pattern. It is assigned to one of the attributes of the DecimalFormat object.

DecimalFormat formatter = new DecimalFormat ("0.0#");

55

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

This is the formatting pattern.

66

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

DecimalFormat formatter = new DecimalFormat ("0.0#");

• The formatting pattern consists of special characters indicating how floating-point values should be formatted.

• The pattern specifies the minimum number of digits before (on the left of) the decimal point. If there are more actual digits before the decimal point they will all be included regardless of the pattern.

77

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

DecimalFormat formatter = new DecimalFormat ("0.0#");

• The pattern specifies the maximum number of digits after the decimal point. If there are more digits after the decimal point than are specified, the value will be rounded to the specified number of digits.

88

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

DecimalFormat formatter = new DecimalFormat ("0.0#");

• The 0 character represents a required digit - if there is no digit in the position a zero is included.

• The # character represents an optional digit - if there is no digit in the position or the digit is a zero, no digit is included.

99

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

• Once you have created an object of the DecimalFormat class, you can call its format method to format a value that is passed as an argument to the method.

• The format method creates a String object containing the characters corresponding to the formatted number and returns the address of the String object.

1010

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

Example:

The formatting pattern below specifies that a zero will precede the decimal point if the number does not have a digit in the one’s position and that the value will be rounded to a maximum of two digits to the right of the decimal point. If the value in the hundredth’s position is a zero the zero will not be displayed.

DecimalFormat formatter = new DecimalFormat ("0.0#");

double f = .6851;String formattedNumber;

formattedNumber = formatter.format(f);

System.out.println(formattedNumber); // Displays 0.69

1111

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

Example:

DecimalFormat formatter = new DecimalFormat ("0.0#");

double g = 4.30;String formattedNumber;

formattedNumber = formatter.format(g);System.out.println(formattedNumber); // Displays 4.3

formattedNumber = formatter.format(57.405);System.out.println(formattedNumber); // Displays 57.4 formattedNumber = formatter.format(612.906);System.out.println(formattedNumber); // Displays 612.91

1212

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

Example:

The formatting pattern below specifies that zeroes will be displayed in the one’s and ten’s position if there are not digits in these positions and that the value will be rounded to exactly two digits to the right of the decimal point.

DecimalFormat formatter = new DecimalFormat("00.00");

double g = 4.30;String formattedNumber;

formattedNumber = formatter.format(g);System.out.println(formattedNumber); // Displays 04.30

1313

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

Example:

DecimalFormat formatter = new DecimalFormat("00.00");

double f = .6851;String formattedNumber;

formattedNumber = formatter.format(f);System.out.println(formattedNumber); // Displays 00.69

formattedNumber = formatter.format(57.405);System.out.println(formattedNumber); // Displays 57.40

formattedNumber = formatter.format(612.906);System.out.println(formattedNumber); // Displays 612.91

1414

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

Example:

The formatting pattern below specifies that a zero will be displayed in the one’s position if there is not a digit in this position, the number will be rounded to a maximum of one digit to the right of the decimal point, but if the digit in the tenth’s position is zero it will not be displayed. Actually, with this pattern when the fractional portion is zero the decimal point is not displayed either. The comma indicates that we want a comma to separate groups of three digits.

DecimalFormat formatter = new DecimalFormat(",##0.#");

String formattedNumber;

formattedNumber = formatter.format(5678.015);

System.out.println(formattedNumber); // Displays 5,678

1515

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

Example:

DecimalFormat formatter = new DecimalFormat(",##0.#");

double f = .6851;double g = 4.30;String formattedNumber;

formattedNumber = formatter.format(f);System.out.println(formattedNumber); // Displays 0.7

formattedNumber = formatter.format(g);System.out.println(formattedNumber); // Displays 4.3

formattedNumber = formatter.format(939.15);System.out.println(formattedNumber); // Displays 939.2

1616

FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS

***See the program DemoDecFormat.java on webct

Try changing the formatting string passed to theconstructor and see what happens.

***See the Java API on the web for information on additional formatting symbols like %, E, and $

1717

THE printf METHOD

• You can use the System.out.printf method to display formatted console output.

• The general format of a call to the printf method is:

System.out.printf(FormatString, ArgumentList);

1818

THE printf METHOD

System.out.printf(FormatString, ArgumentList);

• FormatString is a string that contains text to be displayed and/or formatting specifiers.

• ArgumentList is a list of zero or more additional arguments that are to be formatted according to the format specifiers listed in the format string.

1919

THE printf METHOD

Example:

The printf statement below displays "Go Stars!" on a line on the computer screen like the System.out.print method would.

System.out.printf("Go Stars!\n");

2020

THE printf METHOD

• The FormatString in the previous example did not contain any format specifiers. It was just a simple string of text for display.

• The FormatString can include format specifiers.

• Format specifiers are placeholders for values that are inserted in the string of characters to be displayed.

• Format specifiers identify the data type of the value to be inserted and may include optional formatting controls.

• Format specifiers begin with a percent sign (%) and are followed by a character that represents the data type.

2121

THE printf METHOD

Example:

The %d is the format specifier for a decimal integer. The format specifiers are not displayed. The first %d is a placeholder for the first argument following the FormatString. In this example, the value in number (5), is displayed in the string at the location of the first %d. The second %d is a placeholder for the second argument following the FormatString. The value 10 is displayed at the location of the second %d.

int number = 5;

System.out.printf("The value in number is %d.\nAnother number is %d.\n", number, 10);

2222

THE printf METHOD

The printf statement below produces the output shown.

int number = 5;

System.out.printf("The value in number is %d.\nAnother number is %d.\n", number, 10);

OutputThe value in number is 5.Another number is 10.

2323

THE printf METHOD

Example:

The printf statement in the segment below displays three floating-point values as illustrated. When you want to display a floating-point value, use the %f format specifier in the format string.

double x = 19.2;float y = 13.9554F;

System.out.printf("%f %f %f", x, y, 127.4434); // Note 2 doubles and a float

Output19.200000 13.955400 127.443400

Notice spaces in format string appear in output.

2424

THE printf METHOD

• We can control the number of decimal places of precision displayed after the decimal point in a floating-point value.

Example:

In the code below, the specifier %.2f indicates that the floating-point value should be printed with two digits after the decimal point. The values stored in the variables x and y are not changed, but the values displayed are rounded to two digits of fractional precision.

double x = 19.2;float y = 13.9554F;

System.out.printf("%.2f %.2f %.2f", x, y, 127.4434);

2525

THE printf METHOD

double x = 19.2;float y = 13.9554F;

System.out.printf("%.2f %.2f %.2f", x, y, 127.4434);

Output

19.20 13.96 127.44

Note: the values are rounded to two digits to the right of the decimal point when .2 is

placed between the % and the f.

2626

THE printf METHOD

• The format specifier for displaying a string is %s.

Example:

In the segment below, the specifier %s indicates the location where the string referenced by the variable player is inserted in the string sent as the first argument to the printf method.

String player = "Brendan Morrow";

System.out.printf("%s is my favorite stars player.\n", player);

2727

THE printf METHOD

String player = "Brendan Morrow";

System.out.printf("%s is my favorite stars player.\n", player);

OutputBrendan Morrow is my favorite stars player.

2828

THE printf METHOD

• The format specifier may also specify the minimum field width in which a value is to be displayed.

For example, the specifier %5d says display a decimal integer value in a field that is at least five characters wide. If the value printed requires less than five characters it will be right-justified in the field of five characters (i.e. it will be padded on the left with spaces.) If the value is wider than five characters, the field will be expanded to

display the entire value.

2929

THE printf METHOD

Example:

In the segment below, the two integers in x and y are displayed in a minimum field width of five characters. The value in y cannot be displayed in a field five characters wide so the field is expanded to accommodate the entire value. The floating-point value in f is displayed right-justified in a field ten characters wide.

int x = 27, y = 3456789;float f = 6.74582F;

System.out.printf("x is %5d.\ny is %5d.\n", x, y);System.out.printf("%10.3f\n", f);

3030

THE printf METHOD

The rows of X’s and |’s above a below are just little guides displayed to illustrate the width of the fields. Notice that the decimal point occupies one character width.

int x = 27, y = 3456789;float f = 6.74582F;

System.out.println("XXXX|XXXX|XXXX|XXXX|"); System.out.printf("x is %5d.\ny is %5d.\n", x, y);System.out.printf("%10.3f\n", f);System.out.println("XXXX|XXXX|XXXX|XXXX|");

OutputXXXX|XXXX|XXXX|XXXX|x is 27.y is 3456789. 6.746XXXX|XXXX|XXXX|XXXX|

These two are periods, not decimal points.

3131

THE printf METHOD

*** See the program DemoPrintf.java on webct.

*** See the Java API for more information on printf including more specifiers and flags