Solved

Converting a float to a string

Posted on 2000-03-07
2
276 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 50 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now