strings to floats/integers 
Author Message
 strings to floats/integers

can anyone tell me how to convert a string to a float and/or integer??

i need to go from the form mystring := "123.4567" to the form myfloat
:= 123.4567

or mystring := "12345" to myinteger := 12345

please, I am getting quite desperate. I have looked all through the
ADA95 reference manual, and looked all over the WWW but i cant find
anything.  The closest i have found is Unchecked_Conversion but it
converts things to gibberish so mystring := "3" becomes myfloat :=
0.0E000 etc etc

any info greatly appreciated

Jared

ps. Please email me if possible because i dont have a proper usenet
connection



Sun, 11 Feb 2001 03:00:00 GMT  
 strings to floats/integers


Quote:
>can anyone tell me how to convert a string to a float and/or integer??

>i need to go from the form mystring := "123.4567" to the form myfloat
>:= 123.4567

>or mystring := "12345" to myinteger := 12345

>please, I am getting quite desperate. I have looked all through the
>ADA95 reference manual, and looked all over the WWW but i cant find
>anything.  The closest i have found is Unchecked_Conversion but it
>converts things to gibberish so mystring := "3" becomes myfloat :=
>0.0E000 etc etc

The conversion between strings and float and strings and integers can
easily be accomplished using Ada.Text_IO.   Note that for

         Ada.Text_IO.Integer_IO
   and   Ada.Text_IO.Float_IO

there are Get and Put operations defined for conversion between strings
and the desired numeric type.  Not only is this easy, it is also more
portable than some alternatives.   For some odd reason, this feature of
Ada.Text_IO is frequently undiscovered, even by programmers who have
been using Ada for many years.  

Richard Riehle

http://www.adaworks.com



Sun, 11 Feb 2001 03:00:00 GMT  
 strings to floats/integers

<snip>

Quote:
>The conversion between strings and float and strings and integers can
>easily be accomplished using Ada.Text_IO.   Note that for

>         Ada.Text_IO.Integer_IO
>   and   Ada.Text_IO.Float_IO

>there are Get and Put operations defined for conversion between strings
>and the desired numeric type.  Not only is this easy, it is also more
>portable than some alternatives.   For some odd reason, this feature of
>Ada.Text_IO is frequently undiscovered, even by programmers who have
>been using Ada for many years.

True enough, Richard, but I would only use an instance of a numeric io
generic for the case where I wanted to control the output format.  If I will
accept exponential notation for floating output, I would use the 'image
attribute.  For all conversion from strings, I would use the 'value
attribute.

Am I missing something here?

David C. Hoos, Sr.



Sun, 11 Feb 2001 03:00:00 GMT  
 strings to floats/integers


In response to the following message,

Quote:



>>The conversion between strings and float and strings and integers can
>>easily be accomplished using Ada.Text_IO.   Note that for

>>         Ada.Text_IO.Integer_IO
>>   and   Ada.Text_IO.Float_IO

>>there are Get and Put operations defined for conversion between strings
>>and the desired numeric type.  
>True enough, Richard, but I would only use an instance of a numeric io
>generic for the case where I wanted to control the output format.  If I
will
>accept exponential notation for floating output, I would use the 'image
>attribute.  For all conversion from strings, I would use the 'value
>attribute.
>David C. Hoos, Sr.

 Formatting can often be an important issue. The attribute method,
 while a useful brute-force method of achieving the conversions,
 will not always provide the more discipline result of Get and Put.
 Also, the use of 'Value for an invalid string will raise a constraint
 error.  Would you rather see Ada.Text_IO.Data_Error.  There are many
 other reasons for using the Get and Put, but I know you are aware of
 them.  One anecdote might be useful. In converting floating point
 numbers from a VAX (the sign is in bit 15) to floating point for an
 IBM mainframe (the sign is bit 31) the Get and Put from Float_IO
 served well as a way to properly format the data from one machine to
 another.  The ability to differentiate between data errors and
 constraint errors was important in this situation.

 Richard Riehle

 http://www.adaworks.com



Sun, 11 Feb 2001 03:00:00 GMT  
 strings to floats/integers

Quote:
> The conversion between strings and float and strings and integers can
> easily be accomplished using Ada.Text_IO.   Note that for

>          Ada.Text_IO.Integer_IO
>    and   Ada.Text_IO.Float_IO

> there are Get and Put operations defined for conversion between strings
> and the desired numeric type.

Note that for types Integer and Float, there are predefined
instantations of the associated packages in Text_IO, called

     Ada.Integer_Text_IO
and  Ada.Float_Text_IO

These were included in the language specifically for use by students and
other Ada neophytes.



Wed, 14 Feb 2001 03:00:00 GMT  
 strings to floats/integers
You can use two things called 'image and 'value.

The first converts to a string from another type (e.g.
Put(integer'image(2));)

The second goes the other way (e.g. x := 1 + integer'value("2");)

For your example use myfloat := float'value(mystring);

The only word of warning I would give is that you should be careful that
whatever is in the string can logically represent the type being converted
to. Maybe someone else can enlighten us on what happens if this is not the
case, I can't remember.

Hope this helps,
Bob Fletcher

http://www.users.globalnet.co.uk/~radge/



Quote:
> can anyone tell me how to convert a string to a float and/or integer??

> i need to go from the form mystring := "123.4567" to the form myfloat
> := 123.4567

> or mystring := "12345" to myinteger := 12345

> please, I am getting quite desperate. I have looked all through the
> ADA95 reference manual, and looked all over the WWW but i cant find
> anything.  The closest i have found is Unchecked_Conversion but it
> converts things to gibberish so mystring := "3" becomes myfloat :=
> 0.0E000 etc etc

> any info greatly appreciated

> Jared

> ps. Please email me if possible because i dont have a proper usenet
> connection



Mon, 19 Feb 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. converting strings to floats/integers

2. Converting string to float and integer

3. Converting Strings to Floats and/or Integers

4. Convert Strings Into Numbers (integer, float)

5. convert integer string to date string

6. Integer <-> string and Floating point <-> string conversion

7. Floats to Integers

8. Float, integer conversions

9. floating point number into integer -JavaScript

10. Dr Scheme question: Float to integer conversion?

11. integer to 32-bit float conversion

12. Looking to license integer and floating point arithmetic cores

 

 
Powered by phpBB® Forum Software