22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Simple Data Processing<br />

Writing a Program<br />

2.3.5 Neater Printing<br />

Note that the way that a<br />

number is printed does not<br />

affect how it is stored by the<br />

program, it just tells the<br />

printing method how it is<br />

supposed to be printed.<br />

If you have run any of the above programs you will by now have discovered that the<br />

way in which numbers are printed leaves much to be desired. Integers seem to come<br />

out OK, but floating point numbers seem to have a mind of their own. To get around<br />

this C# provides a slightly different way in which numbers can be printed. This<br />

provides more flexibility, and is also somewhat easier to use if you are printing a<br />

large number of values.<br />

Using Placeholders in Print Strings<br />

A placeholder just marks the place where the value is to be printed. Consider:<br />

int i = 150 ;<br />

double f = 1234.56789 ;<br />

Console.WriteLine ( "i: {0} f: {1}", i, f ) ;<br />

Console.WriteLine ( "i: {1} f: {0}", f, i ) ;<br />

This would print out:<br />

i: 150 f: 1234.56789<br />

i: 150 f: 1234.56789<br />

The {n} part of the string says “parameter number n, counting from 0”. In the second<br />

write statement I have swapped the order of the numbers, but since I've swapped the<br />

order of the parameters too the output is the same.<br />

If I do something mad, for example use {99} to try and get the 99 th parameter, the<br />

WriteLine method will fail with an error. This error will not be picked up by the<br />

compiler however, the program will fail when it runs.<br />

Adjusting real number precision<br />

Placeholders can have formatting information added to them:<br />

int i = 150 ;<br />

double f = 1234.56789 ;<br />

Console.WriteLine ( "i: {0:0} f: {1:0.00}", i, f ) ;<br />

This would print out:<br />

i: 150 f: 1234.57<br />

The 0 characters stand for one or more digits. When placed after a decimal point they<br />

can be used to control the number of decimal places which are used to express a value.<br />

Note that doing this means that if the number is an integer it is printed out as 12.00.<br />

Specifying the number of printed digits<br />

I can specify a particular number of digits by putting in a given number of zeroes:<br />

int i = 150 ;<br />

double f = 1234.56789 ;<br />

Console.WriteLine ( "i: {0:0000} f: {1:00000.00}", i, f );<br />

This would print out:<br />

i: 0150 f: 01234.57<br />

Note that if I do this I get leading zeroes printed out, which is useful if you are printing<br />

things like cheques.<br />

Really Fancy Formatting<br />

If you want really fancy levels of control you can use the # character. A # in the format<br />

string means “put a digit here if you have one”:<br />

int i = 150 ;<br />

double f = 1234.56789 ;<br />

Console.WriteLine ( "i: {0:#,##0} f: {1:##,##0.00}", i, f );<br />

C# <strong>Programming</strong> © Rob Miles 2015 46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!