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,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: James Rogers Subject: Re: Leading zeros with Int_IO.Put()? Or another package? Date: 1996/11/07 Message-ID: <32826BDA.446B@velveeta.apdev.cs.mci.com>#1/1 X-Deja-AN: 195158667 references: <01bbcb50$df9cd480$b259c5c1@cetus> <55shs8$n1f@newsbf02.news.aol.com> content-type: text/plain; charset=us-ascii organization: MCI Telecommunications Colorado Springs, CO mime-version: 1.0 newsgroups: comp.lang.ada x-mailer: Mozilla 2.0 (X11; I; AIX 2) Date: 1996-11-07T00:00:00+00:00 List-Id: 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