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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC 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: "Norman H. Cohen" Subject: Re: Leading zeros with Int_IO.Put()? Or another package? Date: 1996/11/06 Message-ID: <3281088B.24F@watson.ibm.com>#1/1 X-Deja-AN: 194948087 references: content-type: text/plain; charset=us-ascii organization: IBM Thomas J. Watson Research Center mime-version: 1.0 reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada x-mailer: Mozilla 3.0 (Win95; I) Date: 1996-11-06T00:00:00+00:00 List-Id: J.D. Baldwin wrote: > 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? Using the GNAT-specific 'Img attribute and the standard package Ada.Strings.Fixed: Ada.Strings.Fixed.Move (Source => Minute'Img, Target => Buffer, Justify => Right, Pad => '0'); Ada.Text_IO.Put (Buffer); (where Buffer is a string of the appropriate length, in this case 2). Using the standard 'Image attribute (which annoyingly places a space in front of nonnegative numerals) instead of 'Img to retain portability to other compilers: declare Image : constant String := Minute'Image; Buffer : String (1 .. Field_Length); -- 2 in the case of minutes begin Ada.Strings.Fixed.Move (Source => Image (2 .. Image'Length), Target => Buffer, Justify => Right, Pad => '0'); Ada.Text_IO.Put (Buffer); end; -- Norman H. Cohen mailto:ncohen@watson.ibm.com http://www.research.ibm.com/people/n/ncohen