home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol097 / ftostr.txt < prev    next >
Encoding:
Text File  |  1984-04-29  |  5.0 KB  |  224 lines

  1. {prints flt on display to a given length and format}
  2. {could be converted to any text file varible like}
  3. {lst:,myfile etc. with an additional pram}
  4. PROCEDURE ftostr (u:flt; fieldsize:byte; mode:char);
  5.  
  6. var  num$:string 30;   {string to written out}
  7.      sign_u     :integer;
  8.      exp_u      :integer;
  9.      counter    :integer;
  10.      fieldleft  :integer;
  11.      chtoleft   :integer;
  12.      numzeros   :integer;
  13.      uu         :array [1..digmax] of byte;
  14.      
  15. {builds pusedo string too copy from}
  16. procedure fillarray;
  17.  
  18. var  n,m:byte;
  19.  
  20. begin
  21. m:=1;
  22. for n:=1 to dpmax
  23. do   begin
  24.      uu[m]:=(u.dp[n] div 10)+ord('0');
  25.      uu[m+1]:=(u.dp[n] mod 10)+ord('0');
  26.      m:=m+2
  27.      end;
  28. end; {fillarray}
  29.  
  30. {completes string after decimal point}
  31. procedure fillfield;
  32.  
  33. begin
  34. while (fieldleft>0) and (counter<=digmax)
  35. do   begin
  36.      append(num$,chr(uu[counter]));
  37.      fieldleft:=fieldleft-1;
  38.      counter:=counter+1
  39.      end;
  40. while fieldleft>0
  41. do   begin
  42.      append(num$,'0');
  43.      fieldleft:=fieldleft-1
  44.      end;
  45. end; {fillfield}
  46.  
  47. {for engineering type buy it from eric brom}
  48. procedure scitype;
  49.  
  50. var exp$   :string 10;
  51.     radix,x  :integer;
  52.     anyway :boolean;
  53.  
  54. begin
  55. fillarray;
  56. setlength(num$,0);
  57. sign_u:=signdig(u);
  58. exp_u:=expvalue(u);
  59. if sign_u=1
  60. then append(num$,'-');
  61. append (num$,chr(uu[1]));
  62. append (num$,'.');
  63. setlength(exp$,0);
  64. append(exp$,'E');
  65. if exp_u <0
  66. then begin
  67.      append(exp$,'-');
  68.      exp_u:=abs(exp_u);
  69.      end;
  70. radix:=10000;
  71. anyway:=false;
  72. for counter:=1 to 5
  73. do   begin
  74.      if (exp_u>=radix) or anyway
  75.      then begin
  76.           x:=exp_u div radix +ord('0');
  77.           exp_u:=exp_u mod radix;
  78.           radix:=radix div 10;
  79.           append(exp$,chr(x));
  80.           anyway:=true
  81.           end
  82.      else radix:=radix div 10
  83.      end;
  84. x:=fieldsize+1-length(num$)-length(exp$);
  85. for counter :=2 to x
  86. do   append(num$,chr(uu[counter]));
  87. append (num$,exp$);
  88. write(num$)
  89. end; {scitype}
  90.  
  91.  
  92. {this mode tells you as much as possible in the space allotted}
  93. {it drops thru to scitype automatic}
  94. procedure infotype;
  95.  
  96. begin
  97. fillarray;
  98. setlength(num$,0);
  99. sign_u:=signdig(u);
  100. exp_u:=expvalue(u);
  101. fieldleft:=fieldsize;
  102. if sign_u=1
  103. then begin
  104.      append(num$,'-');
  105.      fieldleft:=fieldleft-1;
  106.      end;
  107. chtoleft:=exp_u+1;
  108. if chtoleft>fieldleft
  109. then scitype
  110. else begin
  111.      if chtoleft<1
  112.      then begin
  113.           append(num$,'0.');
  114.           fieldleft:=fieldleft-2;
  115.           numzeros:=0-chtoleft;
  116.           if numzeros>(fieldleft-2)
  117.           then scitype
  118.           else begin
  119.                fieldleft:=fieldleft-numzeros;
  120.                while numzeros>0
  121.                do   begin
  122.                     append(num$,'0');
  123.                     numzeros:=numzeros-1
  124.                     end;
  125.                counter:=1;
  126.                fillfield;
  127.                write(num$)
  128.                end
  129.           end
  130.      else begin
  131.           for counter:=1 to chtoleft
  132.           do append(num$,chr(uu[counter]));
  133.           counter:=chtoleft+1;
  134.           fieldleft:=fieldleft-chtoleft;
  135.           if fieldleft>0
  136.           then begin
  137.                append(num$,'.');
  138.                fieldleft:=fieldleft-1;
  139.                end;
  140.           fillfield;
  141.           write(num$)
  142.           end
  143.      end
  144. end; {infotype}
  145.  
  146. {tries to hold decimal point in a given position but}
  147. {will drop thru to infotype to avoid displaying  0.0000}
  148. procedure fixtype;
  149.  
  150. var  fixval     :integer;
  151.      numblank   :integer;
  152.      holestoleft:integer;
  153.  
  154. begin
  155. fillarray;
  156. setlength(num$,0);
  157. exp_u:=expvalue(u);
  158. sign_u:=signdig(u);
  159. fixval:=ord(mode)-ord('0');
  160. chtoleft:=exp_u+1;
  161. holestoleft:=fieldsize-fixval-sign_u-1;
  162. fieldleft:=fieldsize;
  163. if chtoleft>holestoleft
  164. then infotype
  165. else begin
  166.      if chtoleft<1
  167.      then begin
  168.           numblank:=holestoleft-sign_u-1;
  169.           while numblank>0
  170.           do   begin
  171.                append(num$,' ');
  172.                numblank:=numblank-1
  173.                end;
  174.           if sign_u=1
  175.           then append(num$,'-');
  176.           append(num$,'0.');
  177.           fieldleft:=fixval;
  178.           if (fieldleft+chtoleft)<1
  179.           then infotype
  180.           else begin
  181.                while (chtoleft<0) and (fieldleft>0)
  182.                do   begin
  183.                     append(num$,'0');
  184.                     chtoleft:=chtoleft+1;
  185.                     fieldleft:=fieldleft-1
  186.                     end;
  187.                counter:=1;
  188.                fillfield;
  189.                write(num$)
  190.                end
  191.           end
  192.      else begin
  193.           numblank:=holestoleft-sign_u-chtoleft;
  194.           while numblank>0
  195.           do   begin
  196.                append(num$,' ');
  197.                numblank:=numblank-1;
  198.                end;
  199.           if sign_u=1
  200.           then append(num$,'-');
  201.           counter:=1;
  202.           while chtoleft>0
  203.           do   begin
  204.                append(num$,chr(uu[counter]));
  205.                counter:=counter+1;
  206.                chtoleft:=chtoleft-1
  207.                end;
  208.           append(num$,'.');
  209.           fieldleft:=fixval;
  210.           fillfield;
  211.           write(num$)
  212.           end
  213.      end
  214. end; {fixtype}
  215.  
  216. begin
  217. case mode of
  218.      '0','1','2','3','4','5','6','7','8','9'
  219.           :fixtype; {allows console entry of mode}
  220.      'i','I':infotype;
  221.      else:scitype
  222.    end; {case}
  223. end; {ftostr}
  224.