Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting a float to a string

Posted on 2000-03-07
2
Medium Priority
?
295 Views
Last Modified: 2010-04-02
What is the easiest way to convert a float to a string using code that is compatible with both unix and windows?
Thanks.
0
Comment
Question by:friberg
2 Comments
 
LVL 9

Accepted Solution

by:
jasonclarke earned 200 total points
ID: 2590836
The C Style way is probably to use sprintf:

#include <stdio.h>

float f;
char str[50];
sprintf(str,"%f",f);

then str contains the string rep.

or in C++ style, try:

#include <sstream>
#include <string>
using namespace std;

float f;
ostringstream os;
os << f;
string str = os.str();

both should be portable.
0
 

Author Comment

by:friberg
ID: 2591215
Thanks!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question