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,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a3c22fe6651f1bf7 X-Google-Attributes: gid103376,public From: johnherro@aol.com (John Herro) Subject: Re: Leading zeros with Int_IO.Put()? Or another package? Date: 1996/11/07 Message-ID: <55shs8$n1f@newsbf02.news.aol.com>#1/1 X-Deja-AN: 195089319 sender: root@newsbf02.news.aol.com references: <01bbcb50$df9cd480$b259c5c1@cetus> organization: America Online, Inc. (1-800-827-6364) newsgroups: comp.lang.ada Date: 1996-11-07T00:00:00+00:00 List-Id: baldwin@netcom.com (J.D. Baldwin) wrote: > if (MINUTE <= 9) then > Int_IO.Put(0, 0); > end if; > Int_IO.Put(MINUTE,0); > ... is there a better way to pad out integers > with leading zeros? I don't know if this way is "better," but here's how I force leading zeros when I know the number is never too large for the desired width and the number is never negative. (In this example, Minute is never more than two digits and never negative). Let's call the desired width W. Add the constant 10**W, take the 'Image, which will always have exactly W+2 characters, and select the last W characters of the result. For example, if Minute = 4 and is of type Integer, then Integer'Image(104) is " 104" and the last two characters are "04". In your code, you COULD replace four lines with Ada.Text_IO.Put(Integer'Image(100 + Minute)(3 .. 4)); This technique works only when W is small enough that 10**W plus the largest number you're going to handle doesn't overflow. For example, if the number you're outputting is an Integer and Integer'Last is 2_147_483_647 (ten digits), then W can be at most nine to avoid overflow. Within that limit, this technique can force multiple leading zeros to any desired width. Whether this way of forcing leading zeros is "better" is certainly debatable. All I can say for certain is that it's fewer lines. - John Herro Software Innovations Technology http://members.aol.com/AdaTutor ftp://members.aol.com/AdaTutor ----- ANSWER: "Close cover before striking." QUESTION: What did the labor union leader say to the sewer workers?