Converting a float to a string

Posted on 2000-03-07
Medium Priority
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?
Question by:friberg

Accepted Solution

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

#include <stdio.h>

float f;
char str[50];

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.

Author Comment

ID: 2591215

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.

597 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