From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,22b2c05a8088bbb2 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: Leading zeros with Int_IO.Put()? Or another package? Date: 1996/11/06 Message-ID: #1/1 X-Deja-AN: 194970086 references: organization: The Mitre Corp., Bedford, MA. newsgroups: comp.lang.ada Date: 1996-11-06T00:00:00+00:00 List-Id: In article baldwin@netcom.com (J.D. Baldwin) writes: > I just got a copy of GNAT and 'make'-ed the examples. The first one > I ran, cal.exe, gave me the following date and time: > 11/5/1996 10:4:3 > . . . when what one *wants* of course is: > 11/05/1996 11:04:03 > (Leading zero optional--but, by me, preferred--for the date; omitting > it is of course inexcusable for the minutes and seconds fields in the > time.)... > So I added relevant lines of code to CAL.ADB: > if (MINUTE <= 9) then > Int_IO.Put(0, 0); > end if; > Int_IO.Put(MINUTE,0); > . . . and so forth, for each relevant value. > My question: is there a better way to pad out integers with leading > zeros? What if I wanted to pad to a width of three or four or sixteen, > all with zeros? Is there a standard way to achieve this in Ada? Three answers. 1) Use the ADAR components which are designed specifically to address things like this for Ada 83. 2) In Ada 95 look at Annex F. 3) Write your own Put function and use it instead of the Text_IO.Integer_IO.Put operation. There are several ways to do this cleanly and still get a leading sign or blank if you want, but in this case what you want is: with Int_IO; function Put_Leading(To: out String; Item: in Int_IO.Num) is begin Int_IO.Put(To,Item); for I in To'RANGE loop if To(I) = ' ' then To(I) := '0'; elsif To(I) = '-' and I /= 1 then To(1) := '-'; To(I) := '0'; end if; end loop; end Put_Leading; This may be a little heavy for what you want. (For your case, negative numbers should not occur so moving the sign to the lead position is overkill.) -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...