Util-Valid num enhanced

From Vectorlab

Jump to: navigation, search


Function to get a valid num from a string. This converts to current units, if applicable and expands to recognize unit marks which would be ignored by ValidNumStr, like "ft" or"%". By Orso B. Schmid


{ Orso ************************************************ }
{ Extended call for supporting more units notations than ValidNumStr }
{ ValidNumStr supports notations like "mm" and converts to current units }
FUNCTION U_Str2NumCurrUnits(str : STRING): REAL;
    VAR
        locProc, i : INTEGER;
        temp_s : STRING;
        numCurrUnits : REAL;
        others : ARRAY[1..3,1..2] OF STRING;
 
    BEGIN
        { MIND: U_Str2NumCurrUnits is not inited!!!!!!!! }
        temp_s := str;
        
        { if not valid num, then try if is some other unit flag }
        IF NOT ValidNumStr(str, numCurrUnits) THEN BEGIN
        
            others[1,1] := 'in'; others[1,2] := '"'; { quotation mark }
            others[2,1] := 'ft'; others[2,2] := Chr(39); { apo }
            others[3,1] := '%'; others[3,2] := '/100'; { percent }
            
            FOR i := 1 TO 3 DO BEGIN
                locProc := Pos(others[i,1], str);
                
                IF locProc > 0 THEN BEGIN
                    temp_s := Concat(Copy(str, 1, locProc-1), others[i,2], Copy(str, locProc+2, Len(str)));
                    
                    str := temp_s; 
                END;
            END;
 
            IF ValidNumStr(str, numCurrUnits) THEN
                U_Str2NumCurrUnits := numCurrUnits;
 
        END ELSE
            U_Str2NumCurrUnits := numCurrUnits;
    END;
Personal tools