home *** CD-ROM | disk | FTP | other *** search
- program fmt;
-
- TYPE
- STRING20 = STRING[20];
-
- { *************************************************************************** }
-
- { Turbo function to format real numbers for report output }
-
- { *************************************************************************** }
-
- function fmt ( source : real; mask : string20 ) : string20;
-
- { This function will format a subject real or integer number into a format }
- { as defined by the mask. }
-
- { 9 A digit position of the number, where leading zeroes are output as '0' }
- { Z A digit position of the number, where leading zeroes are output as ' ' }
- { * A digit position of the number, where leading zeroes are output as '*' }
- { V Specifies alignment of the decimal point. If a V is not present, it }
- { is assumed the decimal is to be at the far right }
- { $ A digit position of the number. If more than one '$' appears, the }
- { the digit position closest to the first non-zero digit of the }
- { number is output as a '$'. Leading zeroes are output as spaces. }
- { , A comma appearing before the leading digit is output as a space, }
- { dollar sign, or asterick depending upon the mask. }
- { ' ' All other characters are output unchanged. }
-
- { A negative real number and a mask with leading '9's will inbed the }
- { negative sign in the output number. }
-
- { If there are insufficient significant digits in the mask to format the }
- { number the entire field will be returned filled with '?'. }
-
- var
- count,decimals,
- length_of_mask,
- significant_digit,
- source_position : integer;
- work_str,
- source_str : string20;
- leading_zero,
- floating_dollar,
- decimal_flag : boolean;
- limit : real;
-
- begin
- decimals := 0;
- significant_digit := 0;
- limit := 1;
- floating_dollar := false;
- decimal_flag := false;
- leading_zero := true;
- length_of_mask := length(mask);
- for count := 1 to length_of_mask do
- begin
- case mask [count] of
- 'V' : begin
- significant_digit := succ(significant_digit);
- decimal_flag := true;
- end;
- '9' : begin
- significant_digit := succ(significant_digit);
- if decimal_flag then
- decimals := succ(decimals);
- end;
- 'Z' : begin
- significant_digit := succ(significant_digit);
- if decimal_flag then
- decimals := succ(decimals);
- end;
- '*' : begin
- significant_digit := succ(significant_digit);
- if decimal_flag then
- decimals := succ(decimals);
- end;
- '$' : begin
- floating_dollar := true;
- significant_digit := succ(significant_digit);
- if decimal_flag then
- decimals := succ(decimals);
- end;
- end;
- end;
- for count := 1 to significant_digit - decimals - 1 do
- limit := limit * 10;
- if not decimal_flag then
- limit := limit * 10;
- if source < 0 then
- limit := limit / 10;
- if abs(source) >= limit then
- fillchar(work_str,20,'?')
- else
- begin
- fillchar(work_str,20,' ');
- str(source:significant_digit:decimals,source_str);
- source_position := significant_digit;
- for count := 1 to significant_digit do
- begin
- if leading_zero and (source_str[count] = '0') then
- source_str[count] := ' ';
- if leading_zero and (source_str[count] = '.') then
- leading_zero := false;
- if source_str[count] in ['1'..'9'] then
- leading_zero := false;
- end;
- for count := length_of_mask downto 1 do
- begin
- case mask [count] of
- '9' : begin
- if source_str [source_position] = ' ' then
- work_str [count] := '0'
- else
- work_str [count] := source_str [source_position];
- source_position := pred(source_position);
- end;
- 'Z' : begin
- if source_str [source_position] = ' ' then
- work_str [count] := ' '
- else
- work_str [count] := source_str [source_position];
- source_position := pred(source_position);
- end;
- ',' : begin
- if source_str [source_position] <> ' ' then
- if source_str [source_position] = '-' then
- begin;
- work_str [count] := '-';
- source_str [source_position] := ' ';
- end
- else
- work_str [count] := ','
- else
- case mask [count-1] of
- '9' : work_str [count] := ',';
- 'Z' : work_str [count] := ' ';
- ',' : work_str [count] := ',';
- 'V' : work_str [count] := '0';
- '*' : work_str [count] := '*';
- '$' : begin
- if floating_dollar then
- work_str [count] := '$'
- else
- work_str := ' ';
- floating_dollar := false;
- end;
- end;
- end;
- 'V' : begin
- work_str [count] := '.';
- source_position := pred(source_position);
- end;
- '*' : begin
- if source_str [source_position] = ' ' then
- work_str [count] := '*'
- else
- work_str [count] := source_str [source_position];
- source_position := pred(source_position);
- end;
- '$' : begin
- if (source_str [source_position] = ' ') and floating_dollar then
- begin
- floating_dollar := false;
- work_str [count] := '$'
- end
- else
- work_str [count] := source_str [source_position];
- source_position := pred(source_position);
- end;
- '!'..'~' : { this will bypass previous finds }
- work_str [count] := mask [count];
- end;
- end;
- end;
- work_str [0] := chr(length_of_mask);
- fmt := work_str;
- end;
-
- { END OF FUNCTION FMT(real,mask); ******************************************* }
-
- begin
- writeln(fmt(12345.67,' ZZ,ZZZ'));
- writeln(fmt(12345.67,' ZZ,ZZZV99'));
- writeln(fmt(12345.67,' ZZ,ZZZV9 '));
- writeln(fmt(12345.67,' ZZ,ZZZV99'));
- writeln(fmt(12345.67,' $$$,ZZZ'));
- writeln(fmt(12345.67,'AMT $$$$$$V99'));
- writeln(fmt(12345.67,' $$$,$$$,$$$V99'));
- writeln(fmt(12345.67,' ***,***,***V99'));
- writeln(fmt(12345.67,' Z Z Z Z Z'));
- writeln(fmt(12345.67,'NUMBER - ZZZZZ'));
- writeln(fmt(12345.67,' Z-ZZ-ZZ'));
- writeln(fmt(12345.67,' 999 999 999'));
- writeln(fmt(12345.67,' NO DIGITS! '));
- END.