I am trying to convert an sprintf statement into C++ stream statement.
The sprintf formatting statement I am trying to replicate is "%5.3f"
I am using namespace std and have included and
I have:
double my_double = GetMyDoubleFromSomewhere();
stringstream ss;
ss << ??? << my_double;
I have been looking at the fixed and setprecision, but can't quite figure out how to set the 5 and the 3 of the original formatting specifier?
setprecision and setw would help you.
Don't forget including iomanip
#include <iostream>
#include <iomanip>
#include <stdio.h>
int main(void) {
using namespace std;
double target = 1.2345;
cout << fixed << setw(5) << setprecision(3) << target << endl;;
printf("%5.3f\n", target);
}
You want to use the io manipulators std::setw and std::setprecision, something like:
ss << std::setw(5) << std::setprecision(3) << my_double;
Related
my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double A = 100.35;
cout.precision(0);
cout << std::hexfloat << std::fixed << std::left << A << endl;
return 0;
}
Current output:
100
my expected output:
x64
Explanation:
I want to print the hex value of decimal part of double. But I have been unsuccessful in getting this. need help. Any help in this direction will be appreciated.
What you're asking for is simply not possible. std::hex (the output you're looking for) only works for integral arguments, and std::hexfloat uses an undesirable format. You need to cast or round.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double A = 100.35;
cout.precision(0);
cout << std::hex << std::showbase << std::lround(A) << endl;
return 0;
}
I would like to obtain a number from stringstream and set it to 5 significant figures. How do I do this? So far, this is what I have managed to come up with:
double a = 34.34566535
std::stringstream precisionValue;
precisionValue.precision(6) << a << std::endl;
However, this is not compiling. Thanks.
It doesn't compile because ios_base::precision() returns streamsize (it's an integral type).
You can use stream manipulators:
precisionValue << std::setprecision(6) << a << std::endl;
You'll need to include <iomanip>.
std::stringstream::precision() returns a streamsize, not a reference to the stream itself, which is required if you want to sequence << operators. This should work:
double a = 34.34566535;
std::stringstream precisionValue;
precisionValue.precision(6);
precisionValue << a << std::endl;
The precision member function returns current precision, not a reference to the stringstream, so you cannot chain the calls as you've done in the snippet.
precisionValue.precision(6);
precisionValue << a;
std::cout << precisionValue.str() << std::endl;
Or use the setprecision IO manipulator to chain the calls:
precisionValue << setprecision(6) << a;
std::cout << precisionValue.str() << std::endl;
You can use std::setprecision from header <iomanip>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
int main()
{
double a = 34.34566535;
std::stringstream precisionValue;
precisionValue << std::setprecision(6);
precisionValue << a << std::endl;
std::cout << precisionValue.str();
}
This string operation prints out a double in short-hand, and I can't work out why. Why is this happening, and how can I get the full output like the first line of output?
string myString = "The value is ";
ss.str(""); // stringstream from ealier
ss.clear();
ss << myDouble; // Double with value 0.000014577
myString.append(ss.str());
cout << myDouble << endl;
cout << myString << endl;
$ ./myapp
0.000014577
The value is 1.4577e-05
its default behaviour you should use precision to use fixed precision
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double v = 0.000014577;
cout << fixed << v << endl;
}
Try this:
using std::fixed;
...
ss.setf(fixed);
ss << myDouble;
...
That is because this is the default formatting, you can override it with precision.
I want to know how can I make the string I converted from DWORD to onstringstream and then to AnsiString.
But that doesn't really matter, the conversion could be from int to string, I just want to know how I can make every string converted to ALWAYS show 6 digits, like if my number is 57, in the string it will be 000057.
Thanks!
Use io manipulators setfill and setw:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
std::ostringstream s;
s << std::setfill('0') << std::setw(6) << 154;
std::cout << s.str() << "\n";
return 0;
}
So, the question about formatted output?
you can use iostream::width and `iostream::fill':
// field width
#include <iostream>
using namespace std;
int main () {
cout << 100 << endl;
cout.width(6);
cout.fill('0');
cout << 100 << endl;
return 0;
}
I know how to use printf() to format output of float, for example:
float i = 1;
printf("%.2f", i);
but how to format the output using cout to output 2 digits after "."?
Following will do:
std::cout<<std::fixed<<std::setprecision(2)<<i;
You will also need to include iomanip
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setprecision(2) << setiosflags(ios::fixed) << 3.141592 << endl;
}
use setprecision().
A red herring is to use std::setprecision on its own:
float f = 3.1415;
std::cout << std::setprecision(2) << f;
However, "precision" is not the same as "number of decimal places", as shown in this example.
Fortunately, and somewhat confusingly, switching the stream to "fixed-point" display changes the meaning of std::setprecision to something more akin to "setDecimalPlaces".
So, you can write this:
float f = 3.1415;
std::cout << std::fixed << std::setprecision(2) << f;
Boost format helps:
std::cout << boost::format("%.2f") % i;
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 800000.0/81.0;
cout << setiosflags(ios::fixed) << setprecision(2) << x;
return 0;
}
hope this helps.........
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
double f =3.14159;
cout << setprecision (5) << f << endl;
cout << setprecision (9) << f << endl;
cout << fixed;
cout << setprecision (5) << f << endl;
cout << setprecision (9) << f << endl;
return 0;
}
output..
> 3.1416
> 3.14159
> 3.14159
> 3.141590000
cout << ffmt(0, 2) << i;
Where ffmt is defined as:
class ffmt
{
int width;
int precision;
public:
ffmt( int width, int precision )
: width( width )
, precision( precision )
{
}
friend std::ostream& operator<<(
std::ostream& dest,
ffmt const& manip )
{
dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
dest.width( width );
dest.precision( precision );
}
};
You can also extend this to restore format state at the end of the full
expression; it's not too important for floating point, because most of
the time, you'll use the manipulator in front of each output, but a
manipulator which leaves the stream outputting hex is likely to lead to
surprising output later.
As a general rule, except for quicky demo or test programs, you almost
never use the standard manipulators, other that std::setw. For that
matter, in a lot of applications, you'll even avoid things like ffmt,
in favor of specific manipulators for each semantic value you have.