Formatting - Mathematical & Computer Sciences

Download Report

Transcript Formatting - Mathematical & Computer Sciences

F21SF
Software Engineering Foundations
Formatting
Converting numbers to Strings
and vice versa
Dept of
Computer
Science
13/04/2015
Monica Farrow EM G30
email : [email protected]
Material available on Vision
SJF L3
1
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/essential/io/for
matting.html
13/04/2015
SJF L3
2
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.
13/04/2015
SJF L3
3
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
13/04/2015
SJF L3
4
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
13/04/2015
SJF L3
5
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 ”
13/04/2015
SJF L3
6
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");
13/04/2015
SJF L3
7
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)
13/04/2015
SJF L3
8
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”
13/04/2015
5 + “” => “5”
SJF L3
9
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
13/04/2015
SJF L3
10