comp.lang.ada
 help / color / mirror / Atom feed
* Converting string to float and integer
@ 2000-01-09  0:00 Daniel Platt
  2000-01-09  0:00 ` Matthew Heaney
  2000-01-09  0:00 ` Richard D Riehle
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel Platt @ 2000-01-09  0:00 UTC (permalink / raw)


Hi,

I am tring to convert a string into a float and/or integer.

Ihave tried integer(string), but I get illegal operand for numeric
conversion.

eg code.
answer := Integer(string(1..2));

I am using Gnat Ada (95)

Any help would be greatful.
thanks

Dan.
dan-news@ofdan.co.uk







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

* Re: Converting string to float and integer
  2000-01-09  0:00 Converting string to float and integer Daniel Platt
@ 2000-01-09  0:00 ` Matthew Heaney
  2000-01-09  0:00 ` Richard D Riehle
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 2000-01-09  0:00 UTC (permalink / raw)


In article <3vSd4.35559$E36.436449@news2-hme0> , "Daniel Platt" 
<dan-news@ofdan.co.uk> wrote:

> I am tring to convert a string into a float and/or integer.

Every scalar type comes with predefined attributes to convert from
string to that type, and from that type to a string.

Convert_String_To_Int:
declare
  Image : constant String := "42";
  Value : constant Integer := Integer'Value (Image);
begin
  null;
end Convert_String_To_Int;

Convert_Int_To_String:
declare
  Value : constant Integer := 42;
  Image : constant String := Integer'Image (Value);
begin
  null;
end Convert_Int_To_String;


To convert between Float and String, use Float'Image and Float'Value.

To convert between any scalar type T and String, use T'Image and
T'Value.


--
Help keep evolution in the science classroom and religion out: become a
member of the National Center for Science Education.

<http://www.natcenscied.org/>





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

* Re: Converting string to float and integer
  2000-01-09  0:00 Converting string to float and integer Daniel Platt
  2000-01-09  0:00 ` Matthew Heaney
@ 2000-01-09  0:00 ` Richard D Riehle
  1 sibling, 0 replies; 3+ messages in thread
From: Richard D Riehle @ 2000-01-09  0:00 UTC (permalink / raw)


In article <3vSd4.35559$E36.436449@news2-hme0>,
	"Daniel Platt" <dan-news@ofdan.co.uk> 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




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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-09  0:00 Converting string to float and integer Daniel Platt
2000-01-09  0:00 ` Matthew Heaney
2000-01-09  0:00 ` Richard D Riehle

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