Thread View: comp.lang.pascal.misc
8 messages
8 total messages
Started by Jeff Donovan
Fri, 14 Dec 2001 17:09
Output formatting Question
Author: Jeff Donovan
Date: Fri, 14 Dec 2001 17:09
Date: Fri, 14 Dec 2001 17:09
9 lines
1549 bytes
1549 bytes
Hi,
I am trying to update some code left by a previous programmer, and
not knowing Pascal very well I am stuck. Can someone please tell
me how to add leading zeros to a when outputting an integer. I am trying
to create a hh:mm:ss time string form integer hours, minutes, and
seconds variables.
Thanks in advance for any help,
Jeff Donovan
Re: Output formatting Question
Author: CBFalconer
Date: Fri, 14 Dec 2001 19:07
Date: Fri, 14 Dec 2001 19:07
34 lines
2421 bytes
2421 bytes
Jeff Donovan wrote:
>
> I am trying to update some code left by a previous programmer, and
> not knowing Pascal very well I am stuck. Can someone please tell
> me how to add leading zeros to a when outputting an integer. I am
> trying to create a hh:mm:ss time string form integer hours, minutes,
> and seconds variables.
PROCEDURE write2digs(VAR f : text; n : integer)
BEGIN
n := abs(n); /* just in case */
IF (n < 10) THEN write(f, '0', n : 1)
ELSE write(f, n : 2);
END;
PROCEDURE writedate(VAR f : text; dd, mm, yy, field: integer);
(* in ISO form yyyy-mm-dd with leading zeroes *)
BEGIN
IF field < 10 THEN field := 10;
write(f, yy : field - 6, '-');
write2digs(f, mm);
write(f, '-');
write2digs(f, dd);
END;
Creation of a writetime should be obvious.
--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@XXXXworldnet.att.net)
Available for consulting/temporary embedded and systems.
(Remove "XXXX" from reply address. yahoo works unmodified)
mailto:uce@ftc.gov (for spambots to harvest)
Re: Output formatting Question
Author: Dr John Stockton
Date: Fri, 14 Dec 2001 20:42
Date: Fri, 14 Dec 2001 20:42
27 lines
2590 bytes
2590 bytes
JRS: In article <3C1A3235.5B88630E@marine.usf.edu>, seen in
news:comp.lang.pascal.borland, Jeff Donovan <jeff@marine.usf.edu> wrote
at Fri, 14 Dec 2001 12:09:09 :-
> I am trying to update some code left by a previous programmer, and
>not knowing Pascal very well I am stuck. Can someone please tell
>me how to add leading zeros to a when outputting an integer. I am trying
>to create a hh:mm:ss time string form integer hours, minutes, and
>seconds variables.
Please read Borland's newsgroup guidelines, via mFAQ IIRC. When posting
to c.l.p.misc, you need to indicate dialect; cross-posting in c.l.p.* is
rarely needed.
I use in TP & BP & Delphi something like
function CC(J : word) : string { CC:=00..99 } ; var St : string [2] ;
begin Str(J:2, St) ; if St[1]=#32 then St[1] := '0' ; CC := St end ;
If there is any chance that J may be outside 0-99, use a longer string
and/or a signed parameter.
--
� John Stockton, Surrey, UK. jrs@merlyn.demon.co.uk Turnpike v4.00 MIME. �
<URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
<URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Re: Output formatting Question
Author: Dr John Stockton
Date: Sat, 15 Dec 2001 12:57
Date: Sat, 15 Dec 2001 12:57
26 lines
2481 bytes
2481 bytes
JRS: In article <3C1A4B5C.40DD76DE@yahoo.com>, seen in news:borland.pub lic.turbopascal, CBFalconer <cbfalconer@yahoo.com> wrote at Fri, 14 Dec 2001 19:07:39 :- > PROCEDURE write2digs(VAR f : text; n : integer) > > BEGIN > n := abs(n); /* just in case */ > IF (n < 10) THEN write(f, '0', n : 1) > ELSE write(f, n : 2); > END; IMHO, a routine which can write a wrong value (because of abs) is most undesirable. Just make "n" a word. > PROCEDURE writedate(VAR f : text; dd, mm, yy, field: integer); > (* in ISO form yyyy-mm-dd with leading zeroes *) <G>. Alas, will not give leading year zeroes in the First Millennium - though I doubt whether ISO-8601 requires that! IMHO, a string function is more widely useful than a write procedure. -- � John Stockton, Surrey, UK. jrs@merlyn.demon.co.uk Turnpike v4.00 MIME. � Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links. PAS EXE TXT ZIP via <URL: http://www.merlyn.demon.co.uk/programs/00index.htm> My DOS <URL: http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
Re: Output formatting Question
Author: CBFalconer
Date: Sat, 15 Dec 2001 17:43
Date: Sat, 15 Dec 2001 17:43
46 lines
3017 bytes
3017 bytes
Dr John Stockton wrote: > > JRS: In article <3C1A4B5C.40DD76DE@yahoo.com>, seen in news:borland.pub > lic.turbopascal, CBFalconer <cbfalconer@yahoo.com> wrote at Fri, 14 Dec ^^^^^^^^^^^^^^^ Nope. comp.lang.pascal.misc :-) > > PROCEDURE write2digs(VAR f : text; n : integer) > > > > BEGIN > > n := abs(n); /* just in case */ > > IF (n < 10) THEN write(f, '0', n : 1) > > ELSE write(f, n : 2); > > END; > > IMHO, a routine which can write a wrong value (because of abs) is most > undesirable. Just make "n" a word. If you want to define "word = 0..maxint:" allright. Else it is not portable. I prefer to have a routine usable for many purposes. For signed, we could call it with: IF (n < 0) THEN write(f, '-'); write2digs(f, n); but maybe the name could be improved :-) > > > PROCEDURE writedate(VAR f : text; dd, mm, yy, field: integer); > > (* in ISO form yyyy-mm-dd with leading zeroes *) > <G>. Alas, will not give leading year zeroes in the First Millennium - > though I doubt whether ISO-8601 requires that! > > IMHO, a string function is more widely useful than a write procedure. If you have a system with such extensions. Ok, writedate has a (Y < 1k) bug. We shall restrict its usage to post-Norman-Conquest. I think it also has problems on or after 10,000 AD. -- Chuck F (cbfalconer@yahoo.com) (cbfalconer@XXXXworldnet.att.net) Available for consulting/temporary embedded and systems. (Remove "XXXX" from reply address. yahoo works unmodified) mailto:uce@ftc.gov (for spambots to harvest)
Re: Output formatting Question
Author: Scott Moore
Date: Mon, 17 Dec 2001 22:47
Date: Mon, 17 Dec 2001 22:47
61 lines
2803 bytes
2803 bytes
Right attack. I use a routine like: function digits(n: integer): integer; var c: integer; begin c := 1; if n > 10 then c := c+1; if n > 100 then c := c+1; if n > 1000 then c := c+1; ... [repeated until max power of bits in integer appears ] digits := c end; Then, this general purpose building block can be used to format numbers in various ways. CBFalconer wrote: > > Jeff Donovan wrote: > > > > I am trying to update some code left by a previous programmer, and > > not knowing Pascal very well I am stuck. Can someone please tell > > me how to add leading zeros to a when outputting an integer. I am > > trying to create a hh:mm:ss time string form integer hours, minutes, > > and seconds variables. > > PROCEDURE write2digs(VAR f : text; n : integer) > > BEGIN > n := abs(n); /* just in case */ > IF (n < 10) THEN write(f, '0', n : 1) > ELSE write(f, n : 2); > END; > > PROCEDURE writedate(VAR f : text; dd, mm, yy, field: integer); > (* in ISO form yyyy-mm-dd with leading zeroes *) > > BEGIN > IF field < 10 THEN field := 10; > write(f, yy : field - 6, '-'); > write2digs(f, mm); > write(f, '-'); > write2digs(f, dd); > END; > > Creation of a writetime should be obvious. > > -- > Chuck F (cbfalconer@yahoo.com) (cbfalconer@XXXXworldnet.att.net) > Available for consulting/temporary embedded and systems. > (Remove "XXXX" from reply address. yahoo works unmodified) > mailto:uce@ftc.gov (for spambots to harvest) -- Insert expressive French girl here.
Re: Output formatting Question
Author: Dr John Stockton
Date: Tue, 18 Dec 2001 07:40
Date: Tue, 18 Dec 2001 07:40
45 lines
2662 bytes
2662 bytes
JRS: In article <3C1E75EF.B3A48A0C@cisco.com>, seen in news:borland.pub
lic.turbopascal, Scott Moore <samiam@cisco.com> wrote at Mon, 17 Dec
2001 14:47:11 :-
>function digits(n: integer): integer;
>
>var c: integer;
>
>begin
>
> c := 1;
> if n > 10 then c := c+1;
> if n > 100 then c := c+1;
> if n > 1000 then c := c+1;
>
> ... [repeated until max power of bits in integer appears ]
>
> digits := c
>
>end;
or
function digits(const n : integer) : integer ;
begin
{} if n >= 10000 then digits := 5
else if n >= 1000 then digits := 4
else if n >= 100 then digits := 3
else if n >= 10 then digits := 2
else digits := 1 ;
end ;
. . .
I've given an effective answer for 2-digit numbers. For a variable
number of digits K, if speed not crucial, I'd use a while loop ;
Str(M, St) ;
B := 1 ;
while St[B]=#32 do begin St[B] := '0' ; Inc(B) end ; {??}
--
� John Stockton, Surrey, UK. jrs@merlyn.demon.co.uk Turnpike v4.00 MIME. �
<URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
<URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Re: Output formatting Question
Author: Scott Moore
Date: Tue, 18 Dec 2001 19:32
Date: Tue, 18 Dec 2001 19:32
62 lines
3426 bytes
3426 bytes
comment, this is a great topic, because a lot of folks have this
idea that if the compiler does not supply a built in function for
a particular format, it can't be done. I write the output formatting
functions for IP Pascal completely in standard Pascal. Its slightly
slower than using a "helper" function (like exponent extraction)
but I like the idea that the output functions stand alone.
So if you can write all of the output functions, you can do virtually
any formatting you want. The secret to making it less work is to use
the standard output functions with a little preprocessing as we
have shown here to get exactly want you want with little effort.
Dr John Stockton wrote:
>
> JRS: In article <3C1E75EF.B3A48A0C@cisco.com>, seen in news:borland.pub
> lic.turbopascal, Scott Moore <samiam@cisco.com> wrote at Mon, 17 Dec
> 2001 14:47:11 :-
> >function digits(n: integer): integer;
> >
> >var c: integer;
> >
> >begin
> >
> > c := 1;
> > if n > 10 then c := c+1;
> > if n > 100 then c := c+1;
> > if n > 1000 then c := c+1;
> >
> > ... [repeated until max power of bits in integer appears ]
> >
> > digits := c
> >
> >end;
>
> or
>
> function digits(const n : integer) : integer ;
> begin
> {} if n >= 10000 then digits := 5
> else if n >= 1000 then digits := 4
> else if n >= 100 then digits := 3
> else if n >= 10 then digits := 2
> else digits := 1 ;
> end ;
>
> . . .
>
> I've given an effective answer for 2-digit numbers. For a variable
> number of digits K, if speed not crucial, I'd use a while loop ;
>
> Str(M, St) ;
> B := 1 ;
> while St[B]=#32 do begin St[B] := '0' ; Inc(B) end ; {??}
>
> --
> � John Stockton, Surrey, UK. jrs@merlyn.demon.co.uk Turnpike v4.00 MIME. �
> <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
> <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
> <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
--
Insert expressive French girl here.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads