Task

Express a number in decimal as a fixed-length string with leading zeros.



For example, the number   7.125   could be expressed as   00007.125.




PROGRAM FORMATTED
 
PROCEDURE FORMATTED_PRINT(N,LENGTH,DEC_PLACES->FP$)
 
    LOCAL I,C$,NN$
 
    FORMAT$=STRING$(LENGTH,"#")+"."
 
    FOR I=1 TO DEC_PLACES DO
       FORMAT$=FORMAT$+"#"
    END FOR
 
    OPEN("O",1,"FORMAT.$$$")
       WRITE(#1,FORMAT$;N)
    CLOSE(1)
 
    OPEN("I",1,"FORMAT.$$$")
       INPUT(LINE,#1,N$)
    CLOSE(1)
 
    ! add leading zeros
    FOR I=1 TO LEN(N$) DO
       C$=MID$(N$,I,1)
       IF C$=" " OR C$="%" THEN NN$=NN$+"0" ELSE NN$=NN$+C$
    END FOR
 
    FP$=RIGHT$("000000000000"+NN$,LENGTH) ! chop to required length
 
END PROCEDURE
 
BEGIN
 
   PRINT(CHR$(12);) ! CLS
 
   FOR I=1 TO 10 DO
     N=RND(1)*10^(INT(10*RND(1))-2)
     FORMATTED_PRINT(N,16,5->FP$)
     PRINT("Raw number =";N;TAB(30);"Using custom function =";FP$)
   END FOR
 
END PROGRAM