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, LOTS_OF_MONEY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fc0458fe4be14174 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: Converting string to float and integer Date: 2000/01/09 Message-ID: <85aupn$q9h$1@nntp2.atl.mindspring.net>#1/1 X-Deja-AN: 570491369 References: <3vSd4.35559$E36.436449@news2-hme0> Organization: MindSpring Enterprises X-Server-Date: 9 Jan 2000 21:32:39 GMT Newsgroups: comp.lang.ada Date: 2000-01-09T21:32:39+00:00 List-Id: In article <3vSd4.35559$E36.436449@news2-hme0>, "Daniel Platt" wrote: >Hi, > >I am tring to convert a string into a float and/or integer. Here is a little program I sometimes use in Ada classes to demonstrate the options for satisfying this requirement. -- ========== String_To_Scalar_Demonstration =========== -- String_To_Scalar_Demonstration.adb by Richard Riehle -- This program demonstrates several ways to convert a -- a string to a scalar value. -- -- ===================================================== with Ada.Text_IO; with Ada.Integer_Text_IO; with Ada.Float_Text_IO; use Ada; procedure String_To_Scalar_Demonstration is type Spectrum is (Red, Orange, Yellow, Green, Blue, Indigo, Violet); type Unsigned is mod 2**8; Num : Integer := 0; FNum : Float := 0.0; Color : Spectrum := Blue; MNum : Unsigned := 0; Text : String(1..10); Text_Integer : String := "451"; Text_Float : String := "360.0"; Text_Color : String := "Orange"; Text_Unsigned : String := "42"; Integer_Last : Natural; Float_Last : Natural; Spectrum_Last : Natural; Modular_Last : Natural; 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 Text_IO.Put_Line("The String Values are: "); Text_IO.Put("Orange for Enumerated Type "); Text_IO.Put_Line("451 for Integer Type "); Text_IO.Put("360.0 for Float Type "); Text_IO.Put_Line("42 for Unsigned Type "); Text_IO.New_Line; -- Example 1; using the Value attribute Text_IO.New_Line; Text_IO.Put_Line(" >>>> Example 1; Using 'Value Attribute <<<< "); Color := Spectrum'Value(Text_Color); Num := Integer'Value(Text_Integer); FNum := Float'Value(Text_Float); MNum := Unsigned'Value(Text_Unsigned); SIO.Put(Color); Text_IO.New_Line; IIO.Put(Num); Text_IO.New_Line; FIO.Put(Fnum); Text_IO.New_Line; MIO.Put(MNum); Text_IO.New_Line; 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.Get(From => Text_Integer, Item => Num, Last => Integer_Last); Float_Text_IO.Get(From => Text_Float, Item => FNum, Last => Float_Last); 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 <<<< "); Text_IO.New_Line; SIO.Get(From => Text_Color, Item => Color, Last => Spectrum_Last); MIO.Get(From => Text_Unsigned, Item => MNum, Last => Modular_Last); IIO.Get(From => Text_Integer, Item => Num, Last => Integer_Last); FIO.Get(From => Text_Float, Item => FNum, Last => Float_Last); -- Now Write the Results to the Screen SIO.Put(Item => Color); Text_IO.New_Line; IIO.Put(Item => Num); Text_IO.New_Line; FIO.Put(Item => FNum, Fore => 3, Aft => 3, Exp => 0); Text_IO.New_Line; MIO.Put(Item => MNum); Text_IO.New_Line(2); Text_IO.Put_Line(" **** End of String_To_Scalar_Demonstration **** "); end String_To_Scalar_Demonstration; Hope this is helpful. You may copy it freely and modify it in any way you wish. Richard Riehle http://www.adaworks.com