Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!kirsch@braggvax.arpa From: kirsch@braggvax.arpa (David Kirschbaum) Newsgroups: comp.lang.pascal Subject: Re: Integer conversion Message-ID: <13540@brl-adm.ARPA> Date: 6 May 88 12:41:05 GMT Sender: news@brl-adm.ARPA Lines: 72 The first two functions functions (in Turbo Pascal v3.0, but should work elsewhere) convert numbers to strings. The function RtoS trims the leading space(s) that Turbo puts on a string using the STR(r:8:2, NrStr) setup. The next two procedures work the other way, and include a flashy little error-trapping routine. David Kirschbaum Toad Hall kirsch@braggvax.ARPA *** CUT HERE *** TYPE Str8 = STRING[8]; Str12 = STRING[12]; FUNCTION ItoS(i : INTEGER) : Str8; {convert integer to string.} VAR NrS : Str8; BEGIN STR(i,NrS); {create a working NrS, no padding} ItoS := NrS; END; {of ItoS} FUNCTION RtoS(r : REAL) : Str12; {convert real to string. In this case, I was working with dollars, so that's why just the two decimal points.} VAR NrS : Str12; BEGIN IF r = 0.0 THEN RtoS := '0.00' ELSE BEGIN STR(r:8:2,NrS); {create a working NrS, no padding} WHILE (LENGTH(NrS) > 0) AND (NrS[1] = ' ') DO Delete(NrS,1,1); {trim leading spaces} RtoS := NrS; {return string} END; END; {of RtoS} PROCEDURE Say_BadNr(NrS : Str8; code : INTEGER); {used by StoR and StoI} BEGIN Writeln; Writeln('Incorrect entry.'); Writeln(NrS); {display the response} NrS := ' '; {get a blank line} NrS[code] := '^'; {point to bad char} Writeln(NrS); {display the pointer} END; {of Say_BadNr} PROCEDURE StoR(NrS : Str8; VAR r : REAL; VAR code : INTEGER); {Convert string to real, return global variable code} BEGIN VAL(NrS,r,code); {convert to real} IF code <> 0 THEN BEGIN {error} Say_BadNr(NrS, code); {display error msg} r := 0; {clear the variable} END; END; {of StoR} PROCEDURE StoI(NrS : Str8; VAR i, code : INTEGER); {Convert string to integer, return variables i and code} BEGIN VAL(NrS,i,code); {convert to integer} IF code <> 0 THEN BEGIN {error} Say_BadNr(NrS, code); {display error msg} i := 0; {clear the variable} END; END; {of StoI}