comp.lang.ada
 help / color / mirror / Atom feed
* strings to floats/integers
@ 1998-08-26  0:00 Mr Pinga
  1998-08-26  0:00 ` Richard D Riehle
  1998-09-03  0:00 ` Bob Fletcher
  0 siblings, 2 replies; 6+ messages in thread
From: Mr Pinga @ 1998-08-26  0:00 UTC (permalink / raw)


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





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

* Re: strings to floats/integers
  1998-08-26  0:00 ` Richard D Riehle
@ 1998-08-26  0:00   ` David C. Hoos, Sr.
  1998-08-26  0:00     ` Richard D Riehle
  1998-08-29  0:00   ` Matthew Heaney
  1 sibling, 1 reply; 6+ messages in thread
From: David C. Hoos, Sr. @ 1998-08-26  0:00 UTC (permalink / raw)



Richard D Riehle wrote in message <6s1e9k$bgq@sjx-ixn5.ix.netcom.com>...
>In article <35e41033.9324314@200.223.0.87>,
<snip>

>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.







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

* Re: strings to floats/integers
  1998-08-26  0:00 strings to floats/integers Mr Pinga
@ 1998-08-26  0:00 ` Richard D Riehle
  1998-08-26  0:00   ` David C. Hoos, Sr.
  1998-08-29  0:00   ` Matthew Heaney
  1998-09-03  0:00 ` Bob Fletcher
  1 sibling, 2 replies; 6+ messages in thread
From: Richard D Riehle @ 1998-08-26  0:00 UTC (permalink / raw)


In article <35e41033.9324314@200.223.0.87>,
	joke@haha.com (Mr Pinga) wrote:

>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
richard@adaworks.com
http://www.adaworks.com




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

* Re: strings to floats/integers
  1998-08-26  0:00   ` David C. Hoos, Sr.
@ 1998-08-26  0:00     ` Richard D Riehle
  0 siblings, 0 replies; 6+ messages in thread
From: Richard D Riehle @ 1998-08-26  0:00 UTC (permalink / raw)


In article <6s1g0t$lk6@hacgate2.hac.com>,
	"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:

In response to the following message,

>
>Richard D Riehle wrote in message <6s1e9k$bgq@sjx-ixn5.ix.netcom.com>...
>>In article <35e41033.9324314@200.223.0.87>,
>
>>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
 richard@adaworks.com
 http://www.adaworks.com

 




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

* Re: strings to floats/integers
  1998-08-26  0:00 ` Richard D Riehle
  1998-08-26  0:00   ` David C. Hoos, Sr.
@ 1998-08-29  0:00   ` Matthew Heaney
  1 sibling, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1998-08-29  0:00 UTC (permalink / raw)


Richard D Riehle <laoXhai@ix.netcom.com> writes:

> 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.





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

* Re: strings to floats/integers
  1998-08-26  0:00 strings to floats/integers Mr Pinga
  1998-08-26  0:00 ` Richard D Riehle
@ 1998-09-03  0:00 ` Bob Fletcher
  1 sibling, 0 replies; 6+ messages in thread
From: Bob Fletcher @ 1998-09-03  0:00 UTC (permalink / raw)


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
bob@radge.globalnet.co.uk
http://www.users.globalnet.co.uk/~radge/

Mr Pinga <joke@haha.com> wrote in article
<35e41033.9324314@200.223.0.87>...
> 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
> 
> 




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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-08-26  0:00 strings to floats/integers Mr Pinga
1998-08-26  0:00 ` Richard D Riehle
1998-08-26  0:00   ` David C. Hoos, Sr.
1998-08-26  0:00     ` Richard D Riehle
1998-08-29  0:00   ` Matthew Heaney
1998-09-03  0:00 ` Bob Fletcher

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