{10:3 declaration}????
Author Message
{10:3 declaration}????

program A_Little_Math;

var Index, Count : integer;
Stuff, Thing : real;

begin
Index := 12;
Count := (Index + 4) * (Index - 3);
Writeln('The value of count is now',Count:5);

Stuff := 13.42;
Thing := ((Stuff * Stuff) - 2.456) / (Stuff + 1.3462);
Writeln('The value of thing is now',Thing:10:3); { I don't understand
the 10:3 declaration, can someone explain?}
end.

Tue, 08 Jan 2002 03:00:00 GMT
{10:3 declaration}????

Quote:
> program A_Little_Math;
> var Index, Count : integer;
>     Stuff, Thing : real;
> begin
>    Index := 12; Count := (Index + 4) * (Index - 3);
>    Writeln('The value of count is now',Count:5);
>    Stuff := 13.42;
>    Thing := ((Stuff * Stuff) - 2.456) / (Stuff + 1.3462);
>    Writeln('The value of thing is now',Thing:10:3); { I don't understand
> the 10:3 declaration, can someone explain?}
> end.

It's called Pascal formatting, and it allows the programmer to specify
how the data is displayed on the output.  The general form is:
data_variable:width:decimals
Pascal has default formatting for all data types, and this notation
permits you to override whatever default exists (which is different for
various Pascal implementations).  For example, the default formatting for
a Char variable is 1 space - the space the Char consumes.  However, you
can, if you wish, use 3 spaces by using:
char_variable:3
which will use 3 spaces on the output - 2 blanks and the character
itself.
This formatting permits you to align output formatting, as well as
force the display of precision (the # decimals).
Formatting of real data types can use the 2nd option (decimals), and
that's what you're asking about here.  In the 10:3 example, this states
you wish the real variable to be displayed with a (total) width of 10
spaces, with 3 decimal places shown.  This includes the data value sign,
which is blank for plus.  Without computing the value you've computed
above, here's an example:
Thing := -2147.9876; writeln (Thing:10:3) which displays the following
-2147.988 (note the leading space and the decimal rounding).
Here's a "trick" - writeln (Thing:10:0) produces
-2148 (performs integer rounding as it yields no decimals).
Note further that _whatever_ formatting is used, it's the _minimum_
number of output spaces, and that if the data actually consumes more,
Pascal will use whatever it needs.  Thus, no data is lost (as in some
languages, such as fortran).

Tue, 08 Jan 2002 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages