* Newbie Q: Zero fill in Put
@ 1997-09-01 0:00 Larry Coon
1997-09-02 0:00 ` John Herro
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Larry Coon @ 1997-09-01 0:00 UTC (permalink / raw)
I looked through the docs I have & the FAQ
on Ada Home, but I didn't see an answer to
what I hope is a simple question. How does
one zero-fill for integers in a Put call?
eg:
declare
x: integer := 2;
package int_io is new integer_io(integer);
use int_io;
begin
Put(x);
end;
How do I make it output with leading zeros,
eg: 02, 002, 00000002, etc? The width parameter
seems to only fill with spaces.
Larry Coon
larry@fs2.assist.uci.edu
and lmcoom@home.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie Q: Zero fill in Put
1997-09-01 0:00 Newbie Q: Zero fill in Put Larry Coon
1997-09-02 0:00 ` John Herro
@ 1997-09-02 0:00 ` Jacob Sparre Andersen
1997-09-05 0:00 ` Robert Dewar
1997-09-03 0:00 ` Pascal Obry
` (2 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Jacob Sparre Andersen @ 1997-09-02 0:00 UTC (permalink / raw)
Larry Coon (lmcoon@home.com) wrote:
__________
| I looked through the docs I have & the FAQ
| on Ada Home, but I didn't see an answer to
| what I hope is a simple question. How does
| one zero-fill for integers in a Put call?
^^^^^^^^^^
|^^^^^^^^^^
| How do I make it output with leading zeros,
| eg: 02, 002, 00000002, etc? The width parameter
| seems to only fill with spaces.
|__________
I do not think there is a routine to do it in the standard libraries, but it
is not hard to implement with a few calls to routines in the standard
libraries.
(warning: untested, uncompiled, and probably incorrect code)
with Ada.Strings.Fixed;
with Ada.Text_IO;
generic
type Num is range <>;
Fill : Character := '0';
procedure Put (File : in Ada.Text_IO.File_Type;
Item : in Num;
Fill_To : in Positive) is
use Ada.Strings;
use Ada.Strings.Fixed;
use Ada.Text_IO;
Buffer : String (1 .. Fill_To);
begin -- Put
Move (Target => Buffer,
Source => Trim (Item'Image, Both),
Pad => Fill,
Justify => Right);
Put (File => File,
Item => Buffer);
end Put;
Greetings,
Jacob
--
Jacob Sparre Andersen Web: http://fys.ku.dk/%7Esparre/
Center for Chaos and Turbulence Studies Phone: (+45) 39 65 53 51
The Niels Bohr Institute (+45) 35 32 53 05
--
Try Ada 95 - The programming language of today - and tomorrow!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie Q: Zero fill in Put
1997-09-01 0:00 Newbie Q: Zero fill in Put Larry Coon
@ 1997-09-02 0:00 ` John Herro
1997-09-02 0:00 ` Jacob Sparre Andersen
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: John Herro @ 1997-09-02 0:00 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie Q: Zero fill in Put
1997-09-01 0:00 Newbie Q: Zero fill in Put Larry Coon
1997-09-02 0:00 ` John Herro
1997-09-02 0:00 ` Jacob Sparre Andersen
@ 1997-09-03 0:00 ` Pascal Obry
1997-09-04 0:00 ` Robert Dewar
1997-09-08 0:00 ` Larry Coon
4 siblings, 0 replies; 7+ messages in thread
From: Pascal Obry @ 1997-09-03 0:00 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1429 bytes --]
Another solution is to use the Ada.Text_IO.Editing package.
This package is able to format decimal number with great simplicity
(the functionalities are derived from the COBOL pictures) and is
very powerfull.
This is maybe a complex solution for a simple problem but in some
others complex cases it is very usefull.
-----------------------------------------
with Ada.Text_IO.Editing;
procedure Zfill is
use Ada;
type Num is delta 0.1 digits 10;
package Text_Editing is new Text_IO.Editing.Decimal_Output (Num);
X : Integer := 2;
begin
Text_Editing.Put (Num (X), Pic => Text_IO.Editing.To_Picture ("999"));
end Zfill;
--
--|------------------------------------------------------------
--| Pascal Obry Team-Ada Member |
--| |
--| EDF-DER-IPN-SID- Ing�nierie des Syst�mes d'Informations |
--| |
--| Bureau G1-010 e-mail: pascal.obry@der.edfgdf.fr |
--| 1 Av G�n�ral de Gaulle voice : +33-1-47.65.50.91 |
--| 92141 Clamart CEDEX fax : +33-1-47.65.50.07 |
--| FRANCE |
--|------------------------------------------------------------
--|
--| http://ourworld.compuserve.com/homepages/pascal_obry
--|
--| "The best way to travel is by means of imagination"
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie Q: Zero fill in Put
1997-09-01 0:00 Newbie Q: Zero fill in Put Larry Coon
` (2 preceding siblings ...)
1997-09-03 0:00 ` Pascal Obry
@ 1997-09-04 0:00 ` Robert Dewar
1997-09-08 0:00 ` Larry Coon
4 siblings, 0 replies; 7+ messages in thread
From: Robert Dewar @ 1997-09-04 0:00 UTC (permalink / raw)
<<How do I make it output with leading zeros,
eg: 02, 002, 00000002, etc? The width parameter
seems to only fill with spaces.>>
If you are using GNAT, which fully implements the information systems
annex, then consider using Ada.Text_IO.Editing with an appropriate
picture string.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie Q: Zero fill in Put
1997-09-02 0:00 ` Jacob Sparre Andersen
@ 1997-09-05 0:00 ` Robert Dewar
0 siblings, 0 replies; 7+ messages in thread
From: Robert Dewar @ 1997-09-05 0:00 UTC (permalink / raw)
Jacob says (of outputting numbers with leading zeroes)
<<I do not think there is a routine to do it in the standard libraries, but it
is not hard to implement with a few calls to routines in the standard
libraries.>>
No, that's wrong, there are extensive formatting capabilities defined in
Ada 95 that share the concept of COBOL picture strings. This is a particularly
trivial case (you just use a picture of all 9's). Have a look at the package
Ada.Text_IO.Editing in annex G. This capability is fully implemented in all
current versions of GNAT.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie Q: Zero fill in Put
1997-09-01 0:00 Newbie Q: Zero fill in Put Larry Coon
` (3 preceding siblings ...)
1997-09-04 0:00 ` Robert Dewar
@ 1997-09-08 0:00 ` Larry Coon
4 siblings, 0 replies; 7+ messages in thread
From: Larry Coon @ 1997-09-08 0:00 UTC (permalink / raw)
Thanks to everybody who replied. I got it
working.
Larry Coon
University of California
larry@fs2.assist.uci.edu
and lmcoom@home.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~1997-09-08 0:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-01 0:00 Newbie Q: Zero fill in Put Larry Coon
1997-09-02 0:00 ` John Herro
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox