comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: Leading zeros with Int_IO.Put()?  Or another package?
Date: 1996/11/07
Date: 1996-11-07T00:00:00+00:00	[thread overview]
Message-ID: <55shs8$n1f@newsbf02.news.aol.com> (raw)
In-Reply-To: 01bbcb50$df9cd480$b259c5c1@cetus


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?




  reply	other threads:[~1996-11-07  0:00 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-05  0:00 Collection of 2500+ links about Object-Orientation - interested ? Manfred Schneider
1996-11-07  0:00 ` John Herro [this message]
1996-11-07  0:00   ` Leading zeros with Int_IO.Put()? Or another package? Robert Dewar
1996-11-07  0:00   ` James Rogers
  -- strict thread matches above, loose matches on Subject: below --
1996-11-05  0:00 J.D. Baldwin
1996-11-05  0:00 ` Samuel Tardieu
1996-11-05  0:00 ` David Shochat
1996-11-08  0:00   ` robin
1996-11-08  0:00     ` Ken Garlington
1996-11-08  0:00     ` Robert Dewar
1996-11-10  0:00       ` Verne Arase
1996-11-13  0:00       ` robin
1996-11-13  0:00         ` Ken Garlington
1996-11-19  0:00           ` robin
1996-11-22  0:00             ` Ken Garlington
1996-11-22  0:00             ` Robert Dewar
1996-11-17  0:00         ` Robert Dewar
1996-11-13  0:00       ` robin
1996-11-13  0:00         ` Ken Garlington
1996-11-19  0:00           ` robin
1996-11-19  0:00             ` Robert Dewar
1996-11-20  0:00             ` Norman H. Cohen
1996-11-20  0:00             ` Richard A. O'Keefe
1996-11-20  0:00               ` robin
1996-11-20  0:00                 ` Robert Dewar
1996-11-22  0:00                   ` Richard A. O'Keefe
1996-11-25  0:00                   ` shmuel
1996-11-20  0:00                 ` Richard A. O'Keefe
1996-11-22  0:00                   ` robin
1996-11-22  0:00                     ` Richard A. O'Keefe
1996-11-23  0:00                       ` robin
1996-11-20  0:00                 ` Larry J. Elmore
1996-11-25  0:00                   ` robin
1996-11-25  0:00                     ` Robert Dewar
1996-11-26  0:00                     ` Larry J. Elmore
1996-11-21  0:00                 ` Jerry Coffin
1996-11-22  0:00                 ` Ken Garlington
1996-11-20  0:00               ` Robert Dewar
1996-11-22  0:00             ` Ken Garlington
1996-11-27  0:00               ` Verne Arase
1996-12-02  0:00                 ` Ken Garlington
     [not found]             ` <56tjrh$4a <MPLANET.3294c204jcoffin989a3e@news.rmi.net>
1996-11-24  0:00               ` Bert
1996-11-21  0:00   ` Robert I. Eachus
1996-11-22  0:00     ` robin
1996-11-06  0:00 ` Norman H. Cohen
1996-11-07  0:00   ` Pascal Obry
1996-11-07  0:00     ` Norman H. Cohen
1996-11-08  0:00   ` Norman H. Cohen
1996-11-09  0:00     ` Robert Dewar
1996-11-06  0:00 ` Stephen Leake
1996-11-06  0:00 ` Robert I. Eachus
1996-11-08  0:00   ` Norman H. Cohen
1996-11-08  0:00 ` David Emery
1996-11-24  0:00 ` Fergus Henderson
1996-11-24  0:00   ` Robert Dewar
1996-11-25  0:00     ` Larry Kilgallen
1996-11-25  0:00     ` J. David Bryan
1996-11-27  0:00     ` Verne Arase
1996-11-28  0:00   ` Richard A. O'Keefe
1996-12-15  0:00 Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox