From: James Rogers <jrogers@velveeta.apdev.cs.mci.com>
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: <32826BDA.446B@velveeta.apdev.cs.mci.com> (raw)
In-Reply-To: 55shs8$n1f@newsbf02.news.aol.com
The following certainly does not qualify for the shortest solution,
but it does cover a multitude of cases.
---------------------------------------------------------------------------
-- Utility to print zero-filled strings of integer values.
---------------------------------------------------------------------------
package zero_fill is
procedure zput (Item : in Integer;
Width : in Natural := 2);
function zput ( Item : in Integer;
Width : in Natural := 2) return string;
end zero_fill;
---------------------------------------------------------------------------
-- Utility to print zero-filled strings of integer values.
---------------------------------------------------------------------------
with Ada.Text_IO;
with Ada.Numerics.Aux;
package body zero_fill is
Package tio renames Ada.Text_IO;
Package int_io is new tio.integer_io(integer);
Package naux renames Ada.Numerics.Aux;
procedure zput (Item : in Integer;
Width : in Natural := 2) is
num_digits : long_float;
Length : Positive;
calc_digits : Natural;
begin
num_digits := naux.log(long_float(Item)) / naux.log(10.0);
begin
calc_digits := natural(long_float'floor(num_digits)) + 1;
exception
when constraint_error =>
tio.put_line("Conversion Error.");
raise;
end;
if calc_digits < Width then
Length := Natural(Num_Digits) + 1;
else
Length := Width;
end if;
declare
out_string : string(1..Length);
begin
int_io.put(out_string, Item);
for Index in reverse out_string'Range loop
if out_string(Index) not in '0'..'9' then
out_string(Index) := '0';
end if;
end loop;
tio.put(out_string);
end;
end zput;
function zput ( Item : in Integer;
Width : in Natural := 2) return string is
num_digits : long_float;
Length : Positive;
begin
num_digits := naux.log(long_float(Item)) / naux.log(10.0);
if Natural(num_digits) + 1 < Width then
Length := Natural(Num_Digits) + 1;
else
Length := Width;
end if;
declare
out_string : string(1..Length);
begin
int_io.put(out_string, Item);
for Index in reverse out_string'Range loop
if out_string(Index) not in '0'..'9' then
out_string(Index) := '0';
end if;
end loop;
return out_string;
end;
end zput;
end zero_fill;
--
Jim Rogers
*************************************************************
Team Ada
next prev parent 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 ` Leading zeros with Int_IO.Put()? Or another package? John Herro
1996-11-07 0:00 ` Robert Dewar
1996-11-07 0:00 ` James Rogers [this message]
-- strict thread matches above, loose matches on Subject: below --
1996-11-05 0:00 J.D. Baldwin
1996-11-05 0:00 ` David Shochat
1996-11-08 0:00 ` robin
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 ` Robert Dewar
1996-11-22 0:00 ` Ken Garlington
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 ` 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-20 0:00 ` Norman H. Cohen
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-08 0:00 ` Ken Garlington
1996-11-21 0:00 ` Robert I. Eachus
1996-11-22 0:00 ` robin
1996-11-05 0:00 ` Samuel Tardieu
1996-11-06 0:00 ` Stephen Leake
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 ` 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 ` J. David Bryan
1996-11-25 0:00 ` Larry Kilgallen
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