473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

float to string object in c++, help and discussion...


Ciao,
I'm new in c++ programming and I've experimented some method to convert
real number into c++ string object, at present I've found as best solution
use the C command "sprintf":
//------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

//-- Corpo del programma
int main(int argc, char *argv[])
{
char buff[30];
string risultato;

sprintf(buff,"%3.15f", 1.572757843657862662);
risultato = (string) buff;

cout << risultato << endl;
system("PAUSE");
return 0;
}
//------------------------------------------------------
Output:
1.572757843657863
Premere un tasto per continuare...
//------------------------------------------------------
I've tried another algorithm with "pure" c++ code...

//------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
stringstream buff;
string risultato;
buff << 12.34324435;
buff >> risultato;
cout << risultato << endl;

system("PAUSE");
return 0;
}
//-------------------------------------------------
Output:
12.3432
Premere un tasto per continuare...
-----------------------------------------------------
but I lose several decimal digits....

By the way, several people say it's not good C++ programming to use C code
(for example the Stroustrup's book), but I think it's useful to use
sprintf!!
May somebody tell me if exist a C++ function that works like sprintf and
I've missed???

What do you think about to use C code inside C++???

Thanks,
Checco
Jul 19 '05 #1
3 11561
"Francesco" <ch***********@tin.it> wrote...
[...]
sprintf(buff,"%3.15f", 1.572757843657862662);
[...]
stringstream buff;
string risultato;
buff << 12.34324435;
buff >> risultato;
These two are definitely not the same. Try losing those numbers in
the format string in 'sprintf' (use "%f" instead of "%3.15f") and
see what happens.
-----------------------------------------------------
but I lose several decimal digits....
You don't lose anything. Instead, with 'sprintf' you _specifically_
_gain_ more digits by using the precision (that "15" after the dot).
By the way, several people say it's not good C++ programming to use C code
(for example the Stroustrup's book), but I think it's useful to use
sprintf!!
Nobody is going to tell you what to think. Think whatever you please.
May somebody tell me if exist a C++ function that works like sprintf and
I've missed???
No, there is no "C++ function that works like sprintf". However, if
you want to control how many digits are output to 'cout', see the
description of "I/O manipulators" in your favourite book. Learn about
the available functionality instead of substituting it with something
you already know. Key words here are "setprecision", "setw", "fixed".
What do you think about to use C code inside C++???


I think it is unnecessary.

Victor
Jul 19 '05 #2
On Tue, 29 Jul 2003 18:40:08 +0000, Francesco wrote:

Ciao,
I'm new in c++ programming and I've experimented some method to convert
real number into c++ string object, at present I've found as best solution
use the C command "sprintf":
//------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

//-- Corpo del programma
int main(int argc, char *argv[])
{
char buff[30];
string risultato;

sprintf(buff,"%3.15f", 1.572757843657862662);
risultato = (string) buff;

cout << risultato << endl;
system("PAUSE");
return 0;
}
//------------------------------------------------------
Output:
1.572757843657863
Premere un tasto per continuare...
//------------------------------------------------------
I've tried another algorithm with "pure" c++ code...

//------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
stringstream buff;
string risultato;
buff << 12.34324435;
buff >> risultato;
cout << risultato << endl;

system("PAUSE");
return 0;
}
//-------------------------------------------------
Output:
12.3432
Premere un tasto per continuare...
-----------------------------------------------------
but I lose several decimal digits....


The standard way of doing this is as follows:

template <typename T>
std::string toString(const T &thing) {
std::ostringstream os;
os << thing;
return os.str();
}

This will convert any data type capable of being output into a string.

Jul 19 '05 #3
Thanks Victor!!!
It works perfectly... and I've learned a lot about cout (I'm at the begin
of the book :)
the manipulator are at pag.726 ;) ;)

Thanks to Chris, too
Evenif it was not a solution for my problem (I'm sorry but my english is
terrible and it's very easy to misunderstand me) I've learned from you
about template.

Greetings from Italy ;)
Checco.
On Tue, 29 Jul 2003 14:53:35 -0400, Victor Bazarov <v.********@attAbi.com>
wrote:
"Francesco" <ch***********@tin.it> wrote...
[...]
sprintf(buff,"%3.15f", 1.572757843657862662);
[...]
stringstream buff;
string risultato;
buff << 12.34324435;
buff >> risultato;


These two are definitely not the same. Try losing those numbers in
the format string in 'sprintf' (use "%f" instead of "%3.15f") and
see what happens.
-----------------------------------------------------
but I lose several decimal digits....


You don't lose anything. Instead, with 'sprintf' you _specifically_
_gain_ more digits by using the precision (that "15" after the dot).
By the way, several people say it's not good C++ programming to use C
code
(for example the Stroustrup's book), but I think it's useful to use
sprintf!!


Nobody is going to tell you what to think. Think whatever you please.
May somebody tell me if exist a C++ function that works like sprintf and
I've missed???


No, there is no "C++ function that works like sprintf". However, if
you want to control how many digits are output to 'cout', see the
description of "I/O manipulators" in your favourite book. Learn about
the available functionality instead of substituting it with something
you already know. Key words here are "setprecision", "setw", "fixed".
What do you think about to use C code inside C++???


I think it is unnecessary.

Victor

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
0
by: Robert Oschler | last post by:
Is there a way to use CAST() to convert a string MySQL field to a 'float' value? I know I can convert a string to a SIGNED or UNSIGNED integer value, but not float. I tried FLOAT, DECIMAL, and a...
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
13
by: mishj | last post by:
Someone else on here had the same issue as I have but the response he got didnt solve my problem so I hope someone can help me (for some reason I was unable to reply on the same thread as the...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
5
by: robert | last post by:
Turning algs for old NumPy modules into numpy code I suffer from this: Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
2
by: Peter | last post by:
I have OLE Object field in Access Database. This field contains an array of floats. The array was moved from memory into a string and the string was saved as OLE Object in a database (That was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.