Connecting Tech Pros Worldwide Forums | Help | Site Map

convert string to float

Juhan Voolaid
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi

I'm having hard time finding a way to convert:

std::string input="10.5";
to
float input2;

help is appreciated,
Juhan Voolaid.

Sumit Rajan
Guest
 
Posts: n/a
#2: Jul 23 '05

re: convert string to float



"Juhan Voolaid" <jux@hot.ee> wrote in message
news:cuna0d$7pr$1@kadri.ut.ee...[color=blue]
> Hi
>
> I'm having hard time finding a way to convert:
>
> std::string input="10.5";
> to
> float input2;
>[/color]

Try this link:
http://www.parashift.com/c++-faq-lit...al-issues.html
--
Sumit Rajan <sumit.rajan@gmail.com>


Ron Natalie
Guest
 
Posts: n/a
#3: Jul 23 '05

re: convert string to float


Juhan Voolaid wrote:[color=blue]
> Hi
>
> I'm having hard time finding a way to convert:
>
> std::string input="10.5";
> to
> float input2;[/color]

There are two ways. C gives you strtod which converts between a char array
and double:
input2 = strtod(input.c_str(), NULL);

The C++ streams provide nice conversions to and from a variety of types.
The way to use strings with streams is to use a stringstream:

istringstream in(input);
input >> input2;
Closed Thread