F
Francesco
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
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