1 / 10

F21SF Software Engineering Foundations

Formatting Converting numbers to Strings and vice versa. Dept of Computer Science. F21SF Software Engineering Foundations. Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision. Formatting output.

Download Presentation

F21SF Software Engineering Foundations

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. SJF L3 FormattingConverting numbers to Strings and vice versa Dept of Computer Science F21SFSoftware Engineering Foundations Monica Farrow EM G30 email : M.Farrow@hw.ac.uk Material available on Vision

  2. SJF L3 Formatting output • From java 1.5 onwards, there are formatting features similar to C, which look complicated. You specify the type and the spacing. • This lecture covers an absolute minimum • You’re welcome to find out and use other formatting functionality • If you need further information, see • http://java.sun.com/docs/books/tutorial/java/data/numberformat.html • http://java.sun.com/docs/books/tutorial/essential/io/formatting.html

  3. SJF L3 Formatting output • There is a static format method within the String class. • Call using the class name, like a Math function • The parameters are formatting instructions and the item to be formatted. • A formatted String is returned • You usually need to format a real number, to limit the number of decimal places displayed. • If you are concatenating variables within a sentence, this is the only formatting that you need to do. • However, if you want a table, you can use the width component to fix numbers and text to a fixed width.

  4. SJF L3 Formatting real numbers • In the String format method, the value of the 2nd parameter is returned using the format specified in the 1st parameter String myDistString = String.format("%.1f", distance); If distance contained a double of 296.6666recurring, myDistString = “296.7” • "%.1f" means format a real number to 1 decimal place • % means the string contains formatting information • .1 is the number of decimal places after the . • f means it is a real number

  5. SJF L3 Fixed width decimal numbers • "%-6.2f" • % means the string contains formatting information • - left aligned , default is right aligned • 6 is the minimum width, output will be padded with spaces • .2 is the number of decimal places after the . • f means it is a real number • E.g. String.format(“%6.2f”, 2.33333) => “ 2.33” • E.g. String.format(“%-6.2f”, 2.33333) => “2.33 ” • To print a number within a sentence, omit the minimum width component

  6. SJF L3 Fixed width integers • "%-6d" • % means the string contains formatting information • - left aligned , default is right aligned • 6 is the minimum width, output will be padded with spaces • d means it is an integer number • E.g. String.format(“%6d”, 123) => “ 123” • E.g. String.format(“%-6d”, 123) => “123 ”

  7. SJF L3 Fixed-width text • Use this parameter in the String.format method: %-10s • - left aligned , default is right aligned • 10 is the minimum width, output will be padded with spaces • s means text • The following method call returns "Hello " String.format("%-8s", "Hello"); • The following method call returns " Hello" String.format("%8s", "Hello");

  8. SJF L3 Width known at runtime • You can also create the formatting parameter at run time. E.g. int width = 8; String s = String.format("%"+ width + "s","Hello"); • We could use this approach to centre text within a String (same number of blanks on each side). • To start with, we need to divide the total number of blanks by 2, to find out how many on each side. Use integer division (there might be a remainder)

  9. SJF L3 Converting number to String • Why? Usually, to print or display • You can convert a number to text using the formatting commands String.format • You can also use the String.valueOf command but this will not format it int num = 5; String numText = String.valueOf(num); • If you concatenate a number with a String, the result is a String • 5 + “A” => “5A” 5 + “” => “5”

  10. SJF L3 Converting a String to a number • Why? Usually, if read from a text file or from user input, to use in calculations. From L11 on. • The Integer class has a method to do this. String idtext = “1234”; int id = Integer.parseInt(idtext); • If the text is non-numeric (e.g. “ABCD”), you will get a number format exception • Covered in L09 on exceptions

More Related