Anonymous user
Formatted numeric output: Difference between revisions
no edit summary
(→On the other hand: Agh. Out by one in counting...) |
No edit summary |
||
Line 573:
14> io:fwrite("~9.3.0f~n", [7.125]).
00007.125
</pre>
=={{header|ERRE}}==
<lang ERRE>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</lang>
{{out}}
<pre>
Raw number = 1213.501 Using custom function =0000001213.50100
Raw number = 86886.11 Using custom function =0000086886.11000
Raw number = 7.98853E-03 Using custom function =0000000000.00799
Raw number = 49.03128 Using custom function =0000000049.03128
Raw number = 1072496 Using custom function =0001072496.00000
Raw number = 703.8703 Using custom function =0000000703.87030
Raw number = 9.711614 Using custom function =0000000009.71161
Raw number = 9561278 Using custom function =0009561278.00000
Raw number = 534.9367 Using custom function =0000000534.93670
Raw number = 67121.88 Using custom function =0000067121.88000
</pre>
|