#!/usr/local/rexx/rxx
/*
* FLDFIT
*
* FLDFIT is a program that fits a number you specify into an
* output field of a given length. FLDFIT will convert the number
* into a different format (if necessary) to preserve most of the
* significant digits in the number.
*
* The default output field length coded into the program is 10.
* You can modify the length of the output field by changing the
* 'fldsz' parameter. FLDFIT truncates the least significant digits
* but does NOT round numbers up.
*
* Usage
* -----
* FLDFIT may be typed as a command on the command line
*
* fldfit
*
* or invoked like a function
*
* nnn=fldfit()
*
* where is the number that you want to fit into a field
*
*
* 01/28/93 twg Initial implementation
*
*/
trace o
fldsz = 10 /* output field length -- you may modify this value */
exp = ''
parse source . fhow . /* is this a command or a function? */
parse arg input
input = upper(input) /*can type 'e' instead of 'E' while testing*/
if datatype(input,'N')=0 then do
say '===> Input was not numeric: "'input'"'
outfld='X'
signal endrtn
end
if pos('E',input)=0 then innum = input+0 /* converts to exponential */
/* notation (if necessary) */
else innum = input /* if already has E, don't add to it */
/* (this causes incorrect truncation)*/
/* If num is exponential, reserve space for the Enn in output field */
if pos('E',innum)>0 then do
parse var innum wnum 'E' exp
explen = length(exp)
expoln = explen + 1 /* for the E */
numoln = fldsz - explen
end
else do
wnum = innum
numoln = fldsz
end
/* Truncate to left-most part of field, if too big--else, pad */
/* with blanks at left if number is smaller than field. */
if length(wnum)>numoln then numout = left(wnum,numoln,'0')
else numout = right(wnum,numoln,' ')
if exp='' then outfld = numout
else outfld = numout'E'exp
endrtn:
/* Determine how to return the results */
select
when fhow = 'COMMAND' then do
say '"'outfld'"'
return 0
end
when fhow = 'FUNCTION' then return outfld
when fhow = 'SUBROUTINE' then return outfld
otherwise nop
end
exit