comp.lang.ada
 help / color / mirror / Atom feed
* Number into String
@ 2000-01-06  0:00 Jeong, Lae Hyung
  2000-01-06  0:00 ` David C. Hoos, Sr.
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Jeong, Lae Hyung @ 2000-01-06  0:00 UTC (permalink / raw)


NUM : Integer := 123;

  . . .

How can I convert Integer_value NUM into String_value.


Woud you help me with a simple code?.






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 Number into String Jeong, Lae Hyung
  2000-01-06  0:00 ` David C. Hoos, Sr.
@ 2000-01-06  0:00 ` Jeong, Lae Hyung
  2000-01-06  0:00   ` reason67
  2000-01-06  0:00   ` Robert Dewar
  2000-01-06  0:00 ` Richard D Riehle
  2000-01-06  0:00 ` Jeong, Lae Hyung
  3 siblings, 2 replies; 12+ messages in thread
From: Jeong, Lae Hyung @ 2000-01-06  0:00 UTC (permalink / raw)


    I've found !!

    S : String := Integer'Image(NUM);


"Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote in message
news:8517up$t0m$1@green.kreonet.re.kr...
> NUM : Integer := 123;
>
>   . . .
>
> How can I convert Integer_value NUM into String_value.
>
>
> Woud you help me with a simple code?.
>
>






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 ` Jeong, Lae Hyung
  2000-01-06  0:00   ` reason67
@ 2000-01-06  0:00   ` Robert Dewar
  1 sibling, 0 replies; 12+ messages in thread
From: Robert Dewar @ 2000-01-06  0:00 UTC (permalink / raw)


In article <851a0e$tp7$1@green.kreonet.re.kr>,
  "Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote:
>     I've found !!
>
>     S : String := Integer'Image(NUM);


Good for you (finding this on your own :-). This is by the
way very much an FAQ. Someone the attributes don't show up
on the radar screen as clearly as one would hope, given how
important they are :-)

Now one more thing is that you probably want to learn is:

   S : constant String := Integer'Image (NUM);

Unless you really intend to modify S, the constant should always
be there. In general ALWAYS add the constant keyword to any
object declaration to which it can be applied.


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 ` Jeong, Lae Hyung
@ 2000-01-06  0:00   ` reason67
  2000-01-06  0:00     ` Robert Dewar
  2000-01-06  0:00   ` Robert Dewar
  1 sibling, 1 reply; 12+ messages in thread
From: reason67 @ 2000-01-06  0:00 UTC (permalink / raw)


In article <851a0e$tp7$1@green.kreonet.re.kr>,
  "Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote:
>     I've found !!
>
>     S : String := Integer'Image(NUM);

This thread is kinds fortunate for me to ask a question. I often have
the case where I need to put a number into a string of fixed length. I
have written a routine to do this that I have been using and re-writing
(as I change jobs) since 1989. I was curious if there is an easier way
to do this in Ada 95.

Here are the two routines required:

    --
    -- PROCESSING:
    -- This suprogram returns a copy of the original string with all
    -- the space ' ' characters removed.
    --
    function Strip_Spaces (Str : in String) return String is
        Result : String (1 .. Str'Length);
        Last : Integer := 0;
    begin
        --
        -- Loop through Str and move all characters that are not a ' '
        -- into the Result
        --
        for Index in Str'Range loop
            if Str (Index) /= ' ' then
                Last := Last + 1;
                Result (Last) := Str (Index);
            end if;
        end loop;

        return Result (1 .. Last);
    end Strip_Spaces;

----------
----------

    --
    -- PROCESSING:
    -- This suprogram returns a fixed length string (Length)
    -- representing Number. Any additional space in the returned
    -- string can either be set as a zero ('0') if Show_Leading_Zeros
    -- = True or as a space (' ') if Show_Leading_Zeros = False. If
    -- the length of Number String is greater than the Length provided,
    -- the left most portion of the Number that can fit in the result
    -- is returned.
    --
    function String_Of
                (Number : in Integer;
                 Length : in Integer;
                 Show_Leading_Zeros : in Boolean := True)
    return String is

        Number_String : constant String :=
           Strip_Spaces (Integer'Image (Number));

        Result : String (1 .. Length);

    begin
        --
        -- Fill with leading zero's or spaces as requested
        --
        if Show_Leading_Zeros then
            Result := (others => '0');
        else
            Result := (others => ' ');
        end if;

        --
        -- Assign the number to the end of the string
        --
        if Number_String'Length > Length then

            Result := Number_String (Number_String'Last - Length + 1 ..
                                        Number_String'Last);

        else

            Result (Length - Number_String'Length + 1 .. Length) :=
               Number_String;

        end if;

        return Result;

    end String_Of;

----------
----------

---
Jeffrey Blatt


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00   ` reason67
@ 2000-01-06  0:00     ` Robert Dewar
  2000-01-06  0:00       ` reason67
  0 siblings, 1 reply; 12+ messages in thread
From: Robert Dewar @ 2000-01-06  0:00 UTC (permalink / raw)


In article <852crn$umf$1@nnrp1.deja.com>,
  reason67@my-deja.com wrote:
> In article <851a0e$tp7$1@green.kreonet.re.kr>,
>   "Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote:
> >     I've found !!
> >
> >     S : String := Integer'Image(NUM);
>
> This thread is kinds fortunate for me to ask a question. I
often have
> the case where I need to put a number into a string of fixed
length.


So use the facilities in Text_IO!

Text_IO can output an integer in any width or base that you
like. If you want to convert leading blanks to zeroes, then
either

a) write a trivial routine to do this

b) use the features in the editing packages of annex G if
your compiler supports it (all versions of GNAT support
this annex).

Annex G allows full edited formatting. For example, you can
automatically format the integer 1234567 as

******$12,345.67

if that is what you want.

Your routines seem to be reinventing the wheel here :-)


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00     ` Robert Dewar
@ 2000-01-06  0:00       ` reason67
  2000-01-06  0:00         ` James S. Rogers
  0 siblings, 1 reply; 12+ messages in thread
From: reason67 @ 2000-01-06  0:00 UTC (permalink / raw)


In article <852dmq$vad$1@nnrp1.deja.com>,
  Robert Dewar <robert_dewar@my-deja.com> wrote:
> In article <852crn$umf$1@nnrp1.deja.com>,
>   reason67@my-deja.com wrote:
> > In article <851a0e$tp7$1@green.kreonet.re.kr>,
> >   "Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote:
> > >     I've found !!
> > >
> > >     S : String := Integer'Image(NUM);
> >
> > This thread is kinds fortunate for me to ask a question. I
> often have
> > the case where I need to put a number into a string of fixed
> length.
>
> So use the facilities in Text_IO!

Can't do that. The reason why is stated in the last line of the comment
I supplied at the beginning of my procedure:

     -- If
     -- the length of Number String is greater than the Length
     -- provided, the left most portion of the Number that can fit
     -- in the result is returned.
     --

Text_IO returns a layout_error in this case.

----------
----------

with Ada.IO_Exceptions;
with Ada.Text_IO;

procedure Aaa_Test_It is

    package Int_IO is new Ada.Text_IO.Integer_IO (Integer);

    Number : Integer := 5000;
    Str1   : String (1 .. 3);
begin
    Int_IO.Put (To   => Str1,
                Item => Number);

    Ada.Text_IO.Put_Line (Str1);
exception
    when Ada.IO_Exceptions.Layout_Error =>
        Ada.Text_IO.Put_Line ("Str1 will not work");
end Aaa_Test_It;


I could do the text_io on a larger string than is required and then copy
it into the smaller string, but that is essentially the same thing as I
am doing with the 'image so it does not gain me anything.
---
Jeffrey Blatt



Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 Number into String Jeong, Lae Hyung
@ 2000-01-06  0:00 ` David C. Hoos, Sr.
  2000-01-06  0:00 ` Jeong, Lae Hyung
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: David C. Hoos, Sr. @ 2000-01-06  0:00 UTC (permalink / raw)



Jeong, Lae Hyung <lovelace@dreamwiz.com> wrote in message
news:8517up$t0m$1@green.kreonet.re.kr...
> NUM : Integer := 123;
>
>   . . .
>
> How can I convert Integer_value NUM into String_value.
>
>
> Woud you help me with a simple code?.
>
Every scalar type has an attrinute, 'Image, that returns the
image of its argument's value, as a string.  See the Ada Reference
Manual Section 3.5 for more details.

For your example, Integer'Image (Num) returns " 123".

--
David C. Hoos -- W1DCH
-------------------------------------
The primary purpose of the DATA statement is to give names to
constants; instead of referring to pi as 3.141592653589793 at every
appearance, the variable PI can be given that value with a DATA
statement and used instead of the longer form of the constant.  This
also simplifies modifying the program, should the value of pi change.
                -- FORTRAN manual for Xerox Computers







^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 Number into String Jeong, Lae Hyung
                   ` (2 preceding siblings ...)
  2000-01-06  0:00 ` Richard D Riehle
@ 2000-01-06  0:00 ` Jeong, Lae Hyung
  2000-01-06  0:00   ` Robert A Duff
  3 siblings, 1 reply; 12+ messages in thread
From: Jeong, Lae Hyung @ 2000-01-06  0:00 UTC (permalink / raw)


I've another Question.
 ^^;


NUM : float := 12.3456;
        . . .
How can I convert float number into String ?

help me with simple code !! *^^*..


"Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote in message
news:8517up$t0m$1@green.kreonet.re.kr...
> NUM : Integer := 123;
>
>   . . .
>
> How can I convert Integer_value NUM into String_value.
>
>
> Woud you help me with a simple code?.
>
>






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 ` Jeong, Lae Hyung
@ 2000-01-06  0:00   ` Robert A Duff
  0 siblings, 0 replies; 12+ messages in thread
From: Robert A Duff @ 2000-01-06  0:00 UTC (permalink / raw)


"Jeong, Lae Hyung" <lovelace@dreamwiz.com> writes:

> How can I convert float number into String ?

Use the Float'Image attribute.

- Bob




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00 Number into String Jeong, Lae Hyung
  2000-01-06  0:00 ` David C. Hoos, Sr.
  2000-01-06  0:00 ` Jeong, Lae Hyung
@ 2000-01-06  0:00 ` Richard D Riehle
  2000-01-06  0:00 ` Jeong, Lae Hyung
  3 siblings, 0 replies; 12+ messages in thread
From: Richard D Riehle @ 2000-01-06  0:00 UTC (permalink / raw)


In article <8517up$t0m$1@green.kreonet.re.kr>,
	"Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote:

>NUM : Integer := 123;
>How can I convert Integer_value NUM into String_value.

Mr. Jeong,

Take a look at this little demonstration program I sometimes
use in my Ada classes.  You may freely copy and modify it.

-- ================== Image Demonstration ==============
-- Image_Demonstration.adb     by Richard Riehle
-- This program demonstrates several ways to convert a 
-- a non-textual value to a string of text.
-- 
-- =====================================================
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
use  Ada;
procedure Image_Demonstration is
  type Spectrum is (Red, Orange, Yellow, Green,
                    Blue, Indigo, Violet);
  type Unsigned is mod 2**8;
  Num   : Integer  := 451;
  FNum  : Float    := 360.0;
  Color : Spectrum := Blue;
  MNum  : Unsigned := 42;
  Text  : String(1..10);
  package SIO is new Text_IO.Enumeration_IO(Enum => Spectrum);
  package MIO is new Text_IO.Modular_IO    (Num  => Unsigned);
  package IIO is new Text_IO.Integer_IO    (Num  => Integer);
  package FIO is new Text_IO.Float_IO      (Num  => Float);
begin
  -- Example 1; using the image attribute
  Text_IO.Put_Line(" Example 1; Using 'Image Attribute ");
  Text_IO.Put_Line(Spectrum'Image(Color));
  Text_IO.Put_Line(Unsigned'Image(MNum));
  Text_IO.Put_Line(Integer'Image(Num));
  Text_IO.Put_Line(Float'Image(FNum));
  Text_IO.New_Line;
  -- Example 2; using the procedures of pre-instantiated packages
  Text_IO.Put_Line(" Example 2; using pre-instantiated packages " );
  Integer_Text_IO.Put(Num); Text_IO.New_Line;
  Float_Text_IO.Put  (FNum, Fore => 3, Aft => 3, Exp => 0);  
  Text_IO.New_Line(2);
  -- Example 3; using your own instantiated packages
  Text_IO.Put_Line(" Example 3; Using own instantiations ");
  SIO.Put(Color);   Text_IO.New_Line;
  MIO.Put(MNum);    Text_IO.New_Line;
  IIO.Put(Num);     Text_IO.New_Line;
  FIO.Put(FNum, Fore => 3, Aft => 3, Exp => 0);    
  Text_IO.New_Line(2);
  -- Example 4; convert to text and then print
  Text_IO.Put_Line("Example 4; Convert to text, then print ");
  SIO.Put(To => Text, Item => Color);
  Text_IO.Put_Line(Text);
  MIO.Put(To => Text, Item => MNum);
  Text_IO.Put_Line(Text);
  IIO.Put(To => Text, Item => Num);
  Text_IO.Put_Line(Text);
  FIO.Put(To => Text, Item => FNum, Aft => 3, Exp => 0);
  Text_IO.Put_Line(Text);
  Text_IO.New_Line;
  Text_IO.Put_Line("End of Image_Demonstration ");
end Image_Demonstration;
 
Richard Riehle
http://www.adaworks.com




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
  2000-01-06  0:00       ` reason67
@ 2000-01-06  0:00         ` James S. Rogers
  0 siblings, 0 replies; 12+ messages in thread
From: James S. Rogers @ 2000-01-06  0:00 UTC (permalink / raw)



reason67@my-deja.com wrote in message <852k73$4p7$1@nnrp1.deja.com>...
>In article <852dmq$vad$1@nnrp1.deja.com>,
>  Robert Dewar <robert_dewar@my-deja.com> wrote:
>> In article <852crn$umf$1@nnrp1.deja.com>,
>>   reason67@my-deja.com wrote:
>> > In article <851a0e$tp7$1@green.kreonet.re.kr>,
>> >   "Jeong, Lae Hyung" <lovelace@dreamwiz.com> wrote:
>> > >     I've found !!
>> > >
>> > >     S : String := Integer'Image(NUM);
>> >
>> > This thread is kinds fortunate for me to ask a question. I
>> often have
>> > the case where I need to put a number into a string of fixed
>> length.
>>

If you want truncation then use the Move procedure in Ada.Strings.Fixed

Jim Rogers
Colorado Springs, Colorado






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Number into String
@ 2000-01-06  0:00 mfeldman
  0 siblings, 0 replies; 12+ messages in thread
From: mfeldman @ 2000-01-06  0:00 UTC (permalink / raw)


In a previous article,  Robert A Duff  <bobduff@world.std.com> writes:
>"Jeong, Lae Hyung" <lovelace@dreamwiz.com> writes:
>
>> How can I convert float number into String ?
>
>Use the Float'Image attribute.
>
>- Bob

This is correct but a bit oversimplified. Float'Image returns a string
giving the Float value in "E-notation", and provides no way for the
programmer to specify other formats.

Instead, look up the details of Ada.Float_Text_IO.Put (in RM A.10). 
There are three kinds of Put: to Standard_Output, to a named file,
and to a string. This last form seems to be little known, but is very
handy, because you can use all the formatting parameters: Fore, Aft,
Exp, etc. 

There are Get-from-string and Put-to-string forms in all the various
Ada.Text_IO packages. They are very useful in parsing input and
formatting output!

Mike Feldman


     -----  Posted via NewsOne.Net: Free Usenet News via the Web  -----
     -----  http://newsone.net/ --  Discussions on every subject. -----
   NewsOne.Net prohibits users from posting spam.  If this or other posts
made through NewsOne.Net violate posting guidelines, email abuse@newsone.net




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2000-01-06  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-06  0:00 Number into String Jeong, Lae Hyung
2000-01-06  0:00 ` David C. Hoos, Sr.
2000-01-06  0:00 ` Jeong, Lae Hyung
2000-01-06  0:00   ` reason67
2000-01-06  0:00     ` Robert Dewar
2000-01-06  0:00       ` reason67
2000-01-06  0:00         ` James S. Rogers
2000-01-06  0:00   ` Robert Dewar
2000-01-06  0:00 ` Richard D Riehle
2000-01-06  0:00 ` Jeong, Lae Hyung
2000-01-06  0:00   ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
2000-01-06  0:00 mfeldman

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