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();
Related
I have a code which will convert the float value to string, i have written like below
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
return 0;
}
But the problem is when my value is 10.66 its coming 10.66 but when its 10.80 its coming like 10.8 or when its 10.00 its coming 10 only .
How can i print the complete value
Try this code .
Use the setprecision function with '2' .
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
float myFloat= 10.80;
stringstream stream;
stream << fixed << setprecision(2) << myFloat;
string s = stream.str();
cout<<"value = " << s;
return 0;
}
The trailing zeros are only kept if you set either fixed or scientific mode.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 4.2;
cout << fixed << setprecision(2);
cout << x << endl;
return 0;
}
It seems you want something like below.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << fixed << setprecision(2) << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
}
Probably the least complicated way would be to use printf instead of std::cout. There you can specifically specify how many digits are to be displayed.
#include "stdio.h"
printf("%3.2f",myfloat);
where 3 is the # of digits before and 2 the # of digits after the dot, either can be left out. Append '\n' to the string if you want a new line.
EDIT: Ok, I did not know about setprecision(2).
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 have a problem using atof,
here is the code:
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(){
std::string num ("1.0");
//std::string num ("1.1");
cout<< atof(num.c_str());
return 0;
}
If the num string is "1.1" , it can correctly cout 1.1. But if I want to keep the zero when the num string is "1.0" (want it to be 1.0 but not 1), what should I do?
You need to use std::fixed and std::setprecision, like so:
std::cout<< std::fixed << std::setprecision(1) << atof(num.c_str());
This will require that you include the iomanip header.
A possible solution is
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::cout.precision(3);
std::cout.setf(std::ios::fixed);
std::string s("1.0");
float f = 0.0f;
sscanf(s.c_str(), "%f", &f);
// alternative way of setting this flags
// std::cout << std::fixed << std::setprecision(3) << f << "\n";
std::cout << f << "\n";
return (0);
}
notice that there are at least 2 ways of accomplishing the same format for the output, I left one of them commented out .
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 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";