CSCI 150, Spring 2003
Home | | Course Schedule | | Assignments | | Lecture Notes
1) Default display is in Scientific notation
Variables of type real are decimal numbers. When you write out a real variable,
there are several possible ways the compiler could display the number. The default
display for free pascal is in Scientific notation. In Scientific Notation, a number is
displayed as a decimal multiplied by a power of 10. This often looks more complex
than you would want for most programs. For example, suppose you have the
following code:
var myNumber: real; begin myNumber := 17.45; writeln(myNumber); end.
The number, 17.45, will be displayed something like the following:
1.745000000000000E1
The E1 means that you multiply 1.745 times 10 to the 1st power (or 10).
2) Avoiding the scientific notation display. To avoid scientific notation, you can specify a format for your real numbers by including two numbers after the variable (separated by colons). The first indicates the number of spaces the number will occupy (the "field width" of the number, and the second indicates the number of places after the decimal point that will be shown.
var myNumber: real; begin myNumber := 17.45; writeln(myNumber:10:2); end.
In the above code, the :10 after the variable, myNumber, in the writeln statement indicates that 10 spaces will be allocated for writing out the number. The :2 following the :10 indicates that 2 digits will be displayed after the decimal point. Thus, the above code will display myNumber as:
17.45