Solved

Converting a float to a string

Posted on 2000-03-07
2
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 116
Need some help with mailto 16 45
Finding Divisors 5 61
Winapi.Windows.hpp problem 7 47
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

710 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