Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > convert string to float

Reply
Thread Tools

convert string to float

 
 
Juhan Voolaid
Guest
Posts: n/a
 
      02-13-2005
Hi

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

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

help is appreciated,
Juhan Voolaid.
 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      02-13-2005

"Juhan Voolaid" <(E-Mail Removed)> wrote in message
news:cuna0d$7pr$(E-Mail Removed)...
> Hi
>
> I'm having hard time finding a way to convert:
>
> std::string input="10.5";
> to
> float input2;
>


Try this link:
http://www.parashift.com/c++-faq-lit...al-issues.html
--
Sumit Rajan <(E-Mail Removed)>


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      02-13-2005
Juhan Voolaid wrote:
> Hi
>
> I'm having hard time finding a way to convert:
>
> std::string input="10.5";
> to
> float input2;


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;
 
Reply With Quote
 
pete142 pete142 is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-07-2011
Quote:
Originally Posted by Juhan Voolaid
Hi

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

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

help is appreciated,
Juhan Voolaid.
In C:

char input[] = "10.5";
double num;

num = strtod( input, NULL ); // to double
printf( "%8.3", num ); // back to string

-- pete
 

Last edited by pete142; 02-07-2011 at 09:34 AM.. Reason: fix a mistake
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Convert String to Float if and only if the content of the string aredigits Daniel Firu Ruby 2 01-17-2009 03:30 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Convert float to string Quinn Java 1 08-06-2003 06:01 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments