Rounding off Routine

Author(s): Jon Passler
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n10 (Volume 2, Issue 10)
Date: October 1981
Pages: 15

The 8K ROM’s floating point decimal is great, but sometimes you don’t need 9 decimal places. Use this routine in programs involving dollar amounts to round to 2 decimal places. It also pads empty decimal places with zeros and lines up decimal points.

To adjust decimal places to, for example, three places, change 100 to 1000 in lines 20 and 30, change 2 to 3 and 0 to 00 in line 50, and increase the constant subtracted from L in line 70 by one.

  10 INPUT N
20 LET N1=INT (N*100)
30 IF N*100-N1>=.5 THEN LET N1=N1+1
40 LET N$=STR$ N1
50 IF LEN N$<2 THEN LET N$="0"+N$
60 LET L=LEN N$
70 PRINT TAB 10-L;N$( TO L-2);".";N$(L-1 TO )
80 GOTO 10
Syntactic Sum: 11646, 8K

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.