String to Double conversion C++ [duplicate] - c++

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 8 years ago.
I'm trying to convert a string into a double but my double gets cut off at the 3rd decimal point.
My string looks like this: "-122.39381636393"
After it gets converted it looks like this: -122.394
void setLongitude(string longitude){
this->longitude = (double)atof(longitude.c_str());
cout << "got longitude: " << longitude << endl;
cout << "setting longitude: " << this->longitude << endl;
}
Output example:
got longitude: -122.39381636393
setting longitude: -122.394
I want it to maintain all the decimal points, any tips?

I would write this code if I were you:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "-122.39381636393";
std::cout.precision(20);
cout << "setting longitude: " << stod(str) << endl;
return 0;
}
Basically, you would change things like:
precision for the printing
stod rather than low-level operation to get the double back from the string.
You can see it on ideone running.

It is probably the printing that is truncating precision, not the conversion from string to double.
Look at ios_base::precision http://www.cplusplus.com/reference/ios/ios_base/precision/
e.g.
cout.precision(10);
cout << "setting longitude: " << this->longitude << endl;

The proper C++11 solution is to use stod - String TO Double. You probably should use a try ... catch around that function as it throws an exception if your string is not a valid number.
However, the code you have, using atof is perfectly [assuming no bugs in your particular standard C library] converting to double (despite the name being Ascii TO Float, it returns a double value), you are just not printing enough digits, use precision or setprecision to inform cout how many digits to use, e.g.
cout << "Setting longitude: " << setprecision(15) << this->longitude << endl;
You will need to include <iomanip> for setprecision to work.

Related

Why is stod() shortening the number I am converting from string to double?

I am trying to convert a string to a double, however when i use stod() the double has lost some of it's decimal places.
Here is the relevant code :
cout << line3 << endl;
float temp = stod(line3);
cout << temp << endl;
For example, when line3 is "4.225308642", temp outputs as 4.22531. What is causing the shortening of the number and how can I fix it?
There are two aspects to consider here.
First, formating on a IOStream by default has a precision of 6 significant digits. That explains your result. You can increase the precision with the manipulator setprecision.
Then, float by itself has a limited precision of about 6 decimal digits as well. Although you can display more, they will be the result of displaying a binary float as decimal, not really an increase of the precision. You can get about 15 decimal digits of precision by using double.
So combining the two, the program
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::string line3 = "4.225308642";
std::cout << line3 << '\n';
float tempf = stof(line3);
double tempd = stod(line3);
std::cout << "default: float=" << tempf << ", double=" << tempd << '\n';
std::cout << std::setprecision(20);
std::cout << "precision(20): float=" << tempf << ", double=" << tempd << '\n';
}
has for result:
4.225308642
default: float=4.22531, double=4.22531
precision(20): float=4.2253084182739257812, double=4.2253086419999998924
Note again that the last digits result of displaying a binary format. There is a precision after which you can't expect a decimal representation matching the input, and 20 is greater than that. That aspect is explained in more details here.

Issue with Converting Double to String in C++

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.
Here's a reference for to_string. Note that it produces "As many digits are written as needed to represent the integral part, followed by the decimal-point character and six decimal digits".
The rest of the question is a dupe of lots of SO ansers - see here, for example.
I believe what you are looking for is std::fixed which is described below:
http://www.cplusplus.com/reference/ios/fixed/
Using ostringstream will solve your problem.
#include <sstream>
double var = 1.0000001;
ostringstream os;
os << setprecision(10) << var;
string str = os.str();
cout << str << endl; // this should print 1.0000001

Why does cout truncate a double?

The following is my console input/output.
Please enter a real number: -23486.33
Characters checked: 9
Thank you.
The real number you entered is -23486.3
The value I entered is -23486.33, but yet cout prints it as -23486.3.
The relevant code is below:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// Function prototype (declaration)
string readDouble();
bool isValidDouble(string);
int main()
{
string value;
double number;
value = readDouble();
while (!isValidDouble(value)) {
cout << "The number you entered is not a valid integer." << endl;
value = readDouble();
}
number = atof(value.c_str());
cout << "Thank you." << endl
<< "The real number you entered is " << number << endl;
}
When debugging, I check the value of number right after the method call atof(value.c_str())l;. Number is shown to have a value of -23486.33. So what happens between that and the print out by cout? In no part of my code do I set the precision of cout or make it fixed.
If you have any questions, please let me know.
Have you tried
std::cout << std::setprecision(2) << number;
look at:
http://www.cplusplus.com/reference/iomanip/setprecision/
-23486.3 is displayed because std::cout prints only 6 digits by default.
To print back a number entered from standard input (convertion text → floating number → text), you can use set_precision with digits10 as precision:
double d = -23486.33;
int precision = std::numeric_limits<double>::digits10;
std::cout << std::setprecision(precision) << d << std::endl;
This displays:
-23486.33
To print a number with full precision (usually for convertion floating number → text → floating number), you can use set_precision with max_digits10 as precision:
double d = -23486.33;
int precision = std::numeric_limits<double>::max_digits10;
std::cout << std::setprecision(precision) << d << std::endl;
This displays:
-23486.330000000002
Here the printed number is not the same because -23486.33 doesn't have an exact representation in IEEE encoding (expressed in base 2 instead of base 10).
For more details with digits10 and max_digits10, you can read:
difference explained by stackoverflow
digits10
max_digits10
Set a precision when you output a double and keep precision explicitly when you compare them.
When you convert a string presentation of a DEC number to a double(float point number presentation), the data in the memory might not be mathematically equal to the string presentation. It's the best approximation by a float point number presentation, and vise versa.
You can set the precision to the maximum limit for double.
The code snippet is here:
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;
double number = ... // your double value.
cout << setprecision(numeric_limits<double>::digits10) << number << endl;

Printing floating-point GPA in C++ [duplicate]

This question already has answers here:
Floating point format for std::ostream
(8 answers)
Closed 8 years ago.
my problem is when I am trying to print a floating-point GPA in C++.
It seems like a simple issue, but I can't get it to work. Basically I have a floating point value set to 4.0 for a GPA. However, when I try to print it like this:
cout << gpa << endl;
I get the value of 4. Without the .0 on the end. However, I want the .0 to show up. I have tried setting a precision but with no luck. Any help is appreciated.
You can use std::fixed in conjunction with std::setprecision
#include <iostream> // std::fixed
#include <iomanip> // std::setprecision
int main() {
double gpa = 4.0;
std::cout << std::fixed << std::setprecision(1) << gpa << std::endl;
return 0;
}
// Output is 4.0
#include <iomanip>
...
cout.setf(ios::fixed); // use fixed-point notation
cout.setf(ios::showpoint); // show decimal point
cout.precision(1);
...
cout << gpa << endl;

How do I display more decimals in the output console? [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
I want to output the value of a double in it's full precision. However, when using the cout function, it only displays the first 6 digits even though there is around 15-16 digits of precision.
How do I get my program to display the entire value, including the magnitude (power) component?
Use the setprecision() manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
You can also force scientific notation with the scientific manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/scientific/
cout << scientific << setprecision(15) << my_number << endl;
you could use something like this :
#include <iomanip>
cout << setprecision (9) << double_value << endl;
more iomanipulators, here
You're looking for setprecision (code taken from link):
int main () {
double f =3.14159;
cout << setprecision(15) << f << endl;
return 0;
}