PART I
C h a p t e r 3 :
D a t a T y p e s , L i t e r a l s , a n d V a r i a b l e s
43
PART IPART I
There are three interesting things to notice about this program. First, as you can see,
when a
bool
value is output by
WriteLine( )
, “True” or “False” is displayed. Second, the
value of a
bool
variable is sufficient, by itself, to control the
if
statement. There is no need
to write an
if
statement like this:
if(b == true) ...
Third, the outcome of a relational operator, such as
<
, is a
bool
value. This is why the
expression
10 > 9
displays the value “True.” Further, the extra set of parentheses around
10 > 9
is necessary because the
+
operator has a higher precedence than the
>
.
Some Output Options
Up to this point, when data has been output using a
WriteLine( )
statement, it has been
displayed using the default format. However, the .NET Framework defines a sophisticated
formatting mechanism that gives you detailed control over how data is displayed. Although
formatted I/O is covered in detail later in this book, it is useful to introduce some formatting
options at this time. Using these options, you will be able to specify the way values look
when output via a
WriteLine( )
statement. Doing so enables you to produce more appealing
output. Keep in mind that the formatting mechanism supports many more features than
described here.
When outputting lists of data, you have been separating each part of the list with a plus
sign, as shown here:
Console.WriteLine("You ordered " + 2 + " items at $" + 3 + " each.");
While very convenient, outputting numeric information in this way does not give you any
control over how that information appears. For example, for a floating-point value, you
can’t control the number of decimal places displayed. Consider the following statement:
Console.WriteLine("Here is 10/3: " + 10.0/3.0);
It generates this output:
Here is 10/3: 3.33333333333333
Although this might be fine for some purposes, displaying so many decimal places could
be inappropriate for others. For example, in financial calculations, you will usually want to
display two decimal places.
To control how numeric data is formatted, you will need to use a second form of
WriteLine( )
, shown here, which allows you to embed formatting information:
WriteLine(“
format string
”,
arg0
,
arg1
, ... ,
argN
);
In this version, the arguments to
WriteLine( )
are separated by commas and not + signs. The
format string
contains two items: regular, printing characters that are displayed as-is, and
format specifiers. Format specifiers take this general form:
{
argnum
,
width
:
fmt
}
www.freepdf-books.com
44
P a r t I :
T h e C # L a n g u a g e
Here,
argnum
specifies the number of the argument (starting from zero) to display. The
minimum width of the field is specified by
width,
and the format is specified by
fmt.
The
width
and
fmt
are optional.
During execution, when a format specifier is encountered in the format string, the
corresponding argument, as specified by
argnum,
is substituted and displayed. Thus, the
position of a format specification within the format string determines where its matching
data will be displayed. Both
width
and
fmt
are optional. Therefore, in its simplest form, a
format specifier simply indicates which argument to display. For example,
{0}
indicates
arg0,
{1}
specifies
arg1,
and so on.
Let’s begin with a simple example. The statement
Console.WriteLine("February has {0} or {1} days.", 28, 29);
produces the following output:
February has 28 or 29 days.
As you can see, the value 28 is substituted for
Do'stlaringiz bilan baham: |