1 / 31

Chapter 3 DECISION STRUCTURES CONT’D

Chapter 3 DECISION STRUCTURES CONT’D. 1. 1. FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS. We can use the DecimalFormat class to control the way floating-point numbers are displayed.

jamuna
Download Presentation

Chapter 3 DECISION STRUCTURES CONT’D

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3DECISION STRUCTURES CONT’D 1 1

  2. 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; 2 2

  3. 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#"); 3 3

  4. 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 DecimalFormatclass and also assigning the address of this object to the variable formatter. 4 4

  5. 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#"); FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS This is the formatting pattern. 5 5

  6. 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. 6 6

  7. 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. 7 7

  8. 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. 8 8

  9. 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. 9 9

  10. 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 10 10

  11. 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 11 11

  12. 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 12 12

  13. 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 13 13

  14. 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 14 14

  15. 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 15 15

  16. FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS ***See the program DemoDecFormat.java on webct Try changing the formatting string passed to the constructor and see what happens. ***See the Java API on the web for information on additional formatting symbols like %, E, and $ 16 16

  17. 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); 17 17

  18. 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. 18 18

  19. 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"); 19 19

  20. 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. 20 20

  21. 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); 21 21

  22. 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); Output The value in number is 5.Another number is 10. 22 22

  23. 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 Output Notice spaces in format string appear in output. 19.200000 13.955400 127.443400 23 23

  24. 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); 24 24

  25. THE printf METHOD double x = 19.2; float y = 13.9554F; System.out.printf("%.2f %.2f %.2f", x, y, 127.4434); Output Note: the values are rounded to two digits to the right of the decimal point when .2 is placed between the % and the f. 19.20 13.96 127.44 25 25

  26. 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); 26 26

  27. THE printf METHOD String player = "Brendan Morrow"; System.out.printf("%s is my favorite stars player.\n", player); Output Brendan Morrow is my favorite stars player. 27 27

  28. 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. 28 28

  29. 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); 29 29

  30. 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|"); Output XXXX|XXXX|XXXX|XXXX| x is 27. y is 3456789. 6.746 XXXX|XXXX|XXXX|XXXX| These two are periods, not decimal points. 30 30

  31. THE printf METHOD *** See the program DemoPrintf.java on webct. *** See the Java API for more information on printf including more specifiers and flags 31 31

More Related