December 9th, 2003, 08:35 AM
another question about stl and sprintf
If I have for example a double holding the value 128.65 and I want to present it as 128.650000
than I can do
I tried using your advice and use std::stringstream
but what I managed to do is getting 0000128.65
what I did is:
buffer << std::setw(nLen) << dValue;//nLen is the required length
//in this example it's 10
*psDest = buffer.str();
Can you pls show me how to do that?
Last edited by avi123; December 9th, 2003 at 08:40 AM.
December 9th, 2003, 09:15 AM
double d = 218.65;
December 9th, 2003, 09:20 AM
what do you think is better
using this or using sprintf
(I mean performance)
December 9th, 2003, 10:38 AM
You should only worry about performance if you've discovered a performance problem. Your concern should be maintenance, not performance.
This is better.
December 9th, 2003, 11:00 AM
There's also a manipulator for doing the equivalent of the g, f and e format specifiers, but I can't remember what it is offhand (the default is to output like %g).
using namespace std;
string write_double(double d, int width, int precision)
ss << setw(10) << setprecision(6) << d;
string s = write_double(128.65, 10, 6);
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.