I have a simple program:
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
double i=0.000006;
printf("%lf\n",i);
cout <<i<<endl;
return 0;
}
the output of which is :
pearl.236> ./a.out
0.000006
6e-06
pearl.237>
How can i achieve 0.000006 using the cout too?
the actual proble i have is i am redirecting this double to a string stream and later on i am printing it on screen.i want to know how we can store the actuall double representation inside the string stream.
Stream formatting is achieved with the help of manipulators.
The manipluators to specify standard and scientific notation are fixed and scientific.
cout << fixed <<i<<endl;
Try using std::fixed
std::cout << std::fixed << i << "\n";
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;
}
So I know setprecision(int n) should be used when printing a double value with precision n. However, I've run into a problem on a project that I'm working on that is akin to this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double var = 1.0000001;
cout << setprecision(10)<< var << endl;
string str = to_string(var);
cout << str << endl;
return 0;
}
Here is the output:
1.0000001
1.000000
In the project I'm working on, I need to save the double value as a string, and it will occasionally need more than six decimal places of precision. Here, precision is clearly lost in the conversion. Any pointers would be greatly appreciated.
You can use std::stringstream.
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main(void) {
double var = 1.0000001;
cout << setprecision(10)<< var << endl;
stringstream ss;
ss << setprecision(10) << var;
string str;
ss >> str;
cout << str << endl;
return 0;
}
If you want to get the full precision of your double without limiting it to a specific value (implied by your "occasionally need more than six decimal places"), and if you are using the Boost libraries, you can also try this following alternative:
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace boost;
using namespace std;
int main() {
double var = 1.0000001;
cout << lexical_cast<string>(var) << endl;
return 0;
}
This has proven useful in one of my applications where precision did matter, and where the std::stringstream approach was not very convenient and elegant due to usage of some specific logging function wrappers. You can find more information about boost::lexical_cast and how it deals with internal representations here.
Obviously, if you are not currently using Boost in your project, this approach is overkill.
try this:
#include <sstream>
#include <string>
// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();
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;
Is there a way, using gmplib, to print an mpf_t number without the exponent? I would rather not have to write a function that reads the exponent and moves the decimal manually, as that kind of seems like overkill.
I'm not familiar with gmplib, but does it support the fixed formatting manipulator?
In standard C++:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double d = 1.12345e6;
cout << d << endl;
cout << fixed << d << endl;
return 0;
}
produces:
$ ./test
1.12345e+06
1123450.000000
You can play with the precision with setprecision(n) and width with setw(n) to further tweak the result.
in fact, the GMPLib provide the way to print the mpf_t value well. See follows.
#include <stdio.h>
#include <gmp.h>
int main(int argc, const char * argv[]) {
mpf_t f;
mpf_init_set_str(f, "314.15926", 10);
gmp_printf("%.5Ff\n", f);
return 0;
}
if you run the script,the console will output:
314.15926
Here, the most important thing is about 5 in the GMT float format flag : %.5Ff,which means that it will output the float with 5 digitals
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.