hbn wrote:
> Hi all,
>
> I have some strings that I need to convert to fixed decimal(p, q). The
> strings all have type char(100), but p and q will vary based on
> different fields in the record.
>
> Before assigning the strings to fixed decimal(p, q) variables I would
> like to validate that they are in a proper format, to give som nice
> error messages if they are not.
>
> I know I could let the compiler handle this for me, but that gives
> warnings in the compiler listing and makes it impossible to give nice
> error messages. Therefor I cooked up the following function to
> validate if a string can be converted to a fixed decimal value. Please
> critique it and give me any pointers on how to do this smarter.
>
Try this:
check: proc(x,i,l,p,q) returns(bin fixed(8) unsigned) reorder;
/* return codes: */
/* 0 Valid number: i=start, l=length, p=integer places, q=decimal places */
/* 1 All blanks */
/* 2 Sign but no digits */
/* 3 Non blank character(s) after valid number */
dcl x char(100),(i,j,l,p,q) bin fixed, (substr,verify) builtin;
i,j=verify(x,' ',1); if j=0 then return(1); /* leading blanks */
if substr(x,j,1)='-' then j+=1; else if substr(x,j,1)='+' then j+=1; /* sign */
if j>100 then return(2); /* no digits */
p=j; j=verify(x,'0123456789',j); if j=0 then j=101; p=j-p; /* integer part */
if j=101 then do; q=0; l=j-i; return(0); end; /* no decimal places */
if substr(x,j,1)='.' then do; /* decimal point */
j+=1; q=j; j=verify(x,'0123456789',j); if j=0 then j=101; q=j-q; /* decimal places */
if j=101 then do; l=j-i; return(0); end;
end;
else q=0;
l=j-i; if verify(x,' ',j)=0 then if p+q>0 then return(0); else return(2); else return(3);
end check;
If your compiler doesn't support the third argument of verify, simply change verify(x,p,j) to
verify(substr(x,j),p);