comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: Newbie Q: Zero fill in Put
Date: 1997/09/02
Date: 1997-09-02T00:00:00+00:00	[thread overview]
Message-ID: <19970902134901.JAA25393@ladder02.news.aol.com> (raw)
In-Reply-To: 340B809C.24AE@home.com



Larry Coon <lmcoon@home.com> writes:
> How do I make [integers] output with
> leading zeros, eg: 02, 002, 00000002, etc?
> The width parameter seems to only fill with
> spaces.
     Here's one way to do it that works in Ada 83 as well as Ada 95.  Let
J be the integer and W be the width that you want.  I assume the integer is
non-negative and that it will fit in W spaces.  Add J to 10**W (here I
assume that this will not overflow).  Take the Integer'Image of the result,
which will always have a length of W+2 because of the leading space. 
Select the slice (3 .. W+2).
     For example, if J is 2 and you want a width of 3, this will print 002:
Ada.Text_IO.Put(Integer'Image(1000 + J)(3 .. 5));
     Obviously, this works with any integer type.  For example, many
compilers have Long_Integer.
     There are other ways to do what you want, but this way is quick and
simple if you know that J will never be large enough to cause an overflow. 
I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




  reply	other threads:[~1997-09-02  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-01  0:00 Newbie Q: Zero fill in Put Larry Coon
1997-09-02  0:00 ` John Herro [this message]
1997-09-02  0:00 ` Jacob Sparre Andersen
1997-09-05  0:00   ` Robert Dewar
1997-09-03  0:00 ` Pascal Obry
1997-09-04  0:00 ` Robert Dewar
1997-09-08  0:00 ` Larry Coon
replies disabled

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