std::string to float
-
can anyone tell me how to convert a std::string to float type?
-
can anyone tell me how to convert a std::string to float type?
float MyFloat = atof(MyString.c_str());
-
can anyone tell me how to convert a std::string to float type?
From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
Michael Dunn wrote:
From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]
Please no stringstream solution. Just encapsulate strtod.
-
Michael Dunn wrote:
From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]
Please no stringstream solution. Just encapsulate strtod.
:confused: Sorry for providing a pointer to a FAQ with a solution to the OP's problem.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
:confused: Sorry for providing a pointer to a FAQ with a solution to the OP's problem.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Michael Dunn wrote:
From the C++ FAQ lite: [39.2] How do I convert a std::string to a number?[^]
Please no stringstream solution. Just encapsulate strtod.
-
I would like to know why not too. While MSVC6s STL implementation is crap and it would be more efficient to use the old C-style functions, other STL versions are more efficient and have less overhead. Efficiency issues aside, there are many advantages to the
stringstream
solutions such as that fact that it's extensible and you can change the type your converting to simply by changing variable your reading into instead of having to change the function called. The intent is also clearer as the C style function names are obscure to say the least. Another issue is the fact that STL implementations are allowed to implementbasic_string
in a manner such that callingc_str
is not as efficient as the programmer might think. One example isbasic_string
implementations which don’t use a single contiguous memory block to store the string. Steve -- modified at 21:50 Thursday 4th May, 2006 -
float MyFloat = atof(MyString.c_str());
float MyFloat=atoi(MyString.c_str()); That's correct!;P
-
float MyFloat=atoi(MyString.c_str()); That's correct!;P
:confused: Err, no, this will convert a string to an integer value, not to a float value. atof convert to a float value.
-
:confused: Sorry for providing a pointer to a FAQ with a solution to the OP's problem.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
Gee Mike, what in the world were you thinking? :rolleyes:
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb