Presentation is loading. Please wait.

Presentation is loading. Please wait.

05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30

Similar presentations


Presentation on theme: "05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30"— Presentation transcript:

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

2 05/09/2015SJF L32 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/num berformat.html http://java.sun.com/docs/books/tutorial/java/data/num berformat.html http://java.sun.com/docs/books/tutorial/essential/io/for matting.html http://java.sun.com/docs/books/tutorial/essential/io/for matting.html

3 05/09/2015SJF L33 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 05/09/2015SJF L34 Formatting real numbers In the String format method, the value of the 2 nd parameter is returned using the format specified in the 1 st 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 05/09/2015SJF L35 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 05/09/2015SJF L36 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 05/09/2015SJF L37 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 05/09/2015SJF L38 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 05/09/2015SJF L39 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 05/09/2015SJF L310 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


Download ppt "05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30"

Similar presentations


Ads by Google