Rounding real number to half thousands

{ ========
Newsgroups: comp.lang.pascal.borland
Subject: Rounding off a real number to half thousands
From: allan...@pennet.net (Allan Bremer)
Date: 18 Feb 1996 13:01:19 GMT

Hello,

I am trying to round off real numbers (or a string) to half
thousands.

example:  34.574423  to  34.5745
           3.123164  to   3.1230
           0.012345  to   0.0120

You see, there rounded off to the nearest half thousand.  How can I
do this using Pascal.

Thank you

Depending on where you wish to generate the 4 place accuracy fixed
decimal number, here are a few ways to do it. The ouput was stored

in a disk file named TMPJUNK.TXT which is as follows:
<clifp...@airmail.net>

 ********** DISK FILE TMPJUNK.TXT ******

The line below is with format type rounding.
 34.5744    3.1232    0.0123

The next line uses ROUND(x * 1E4) / 1E4
to eliminate following decimals from
future calculations but it can still be
formatted for fixed decimal output, as shown

 3.4574400000E+01    3.1232000000E+00    1.2300000000E-02
 34.5744             3.1232              0.0123

which is generated by the following program:  }

PROGRAM ROUNDING;
USES CRT;   {just to clear the screen}

CONST
r1 = 34.574423 ;
r2 = 3.123164 ;
r3 = 0.012345 ;
filename = 'TMPJUNK.TXT';

VAR
filevar : text;
x, y, z: real;
s: string;

FUNCTION Round4(re:real):real;
Begin
     Round4 := ROUND( re * 1E4) / 1E4 ;
End;

BEGIN
     ClrScr;
     Writeln(r1, '   ', r2, '   ', r3);
     x := r1;    y := r2;   z :=r3;
     Writeln; Writeln('Format type rounding to the screen');
     Writeln (x:8:4, y:10:4, z:10:4);

     Writeln; Writeln('<<<Press Enter>>>');
     Readln;

{     Writeln(Lst, x:8:4, y:10:4..etc with USES PRINTER for hard copy}

     Assign(filevar, filename);
     Rewrite(filevar);
     Writeln(filevar, ' ********** DISK FILE TMPJUNK.TXT ****** ');
     Writeln(filevar);
     Writeln(filevar, 'The line below is with format type rounding.');
     Writeln(filevar, x:8:4, y:10:4, z:10:4);
     Writeln(filevar);
     Writeln(filevar, 'The next line uses ROUND(x * 1E4) / 1E4');
     Writeln(filevar, 'to eliminate following decimals from');
     Writeln(filevar, 'future calculations but it can still be');
     Writeln(filevar, 'formatted for fixed decimal output, as shown');
     Writeln(filevar);

     x := Round4(x);     y := Round4(y);      z := Round4(z);
     Writeln(filevar, x, '   ', y, '   ',z);
     Writeln(filevar, x:8:4, y:19:4, z:20:4);
     Close(filevar);

     Reset(filevar);
     While Not EOF(filevar) Do
           Begin
                Readln(filevar, s);
                Writeln(s);
           End;
      Close(filevar);
      Erase(filevar);
      Writeln; Writeln('<<<Press Enter>>>');
      Readln;
end.