I have a string variable and want to convert it into double without any scientific notation. I tried using std::stod but that doesn't work.
std::stringstream timestamp;
timestamp << t_val;
string test = timestamp.str();
cout << test << endl; // this gives 1506836639.96
double d = std::stod(test);
cout << d << endl; // This gives 1.50684e+09 instead of 1506836639.96
I tried using setprecision and fixed but I couldn't store the result into a variable. Is there a way I can store the value of test (1506836639.96) as a double?
The scientific notation has to do with std::cout, not the way the value is stored, so you must use std::fixed before you print the value:
std::cout << std::fixed << std::setprecision(2) << d << std::endl;
As you can see in the demo, this works fine, it should work for you as well.
As #goodvibration commented std::to_string also works but it's not possible to redefine, in a simple manner, the default number or decimal places in this case.
std::cout << std::to_string(d) << std::endl;
Live demo
Related
double calc_percent_of_daily_sugar(int sugar) {
double result;
result = sugar/avg_sugar_intake;
return result*100;
}
I'm trying to get this function to return the result to only 2 decimal places however when I try to use cout << setprecision(2) << result; then there are still more than 2 places in the number
Apologies if this is a nooby or stupid question.
*How to print a “double” value to two decimal places in base 10 in C++
Use std::fixed to print it in decimal.
std::cout << std::setprecision(2) << std::fixed << 123.4567 << std::endl;
// 123.46
See: https://en.cppreference.com/w/cpp/io/manip/fixed
I am looking for a way to convert double to string in c++ such that total number of digits remain to be 10 irrespective of how many are present before and after the decimal point and irrespective of the zeroes. Examples for better understanding:
0.00000000000000000000 =expected outcome> 0.0000000000
12345.00000000000000000 =expected outcome> 12345.00000
-15.123456789012 =expected outcome> -15.12345678
I couldnt find any relevant answer. For the methods like snprintf, std::setprecision with ostringstream, to_string, Boost's lexical_cast, some of the above case fails.
Example of code:
double num = 12345.0000000000001;
std::ostringstream streamObj2;
streamObj2 << std::fixed << std::setprecision(10) << num;
std::string strObj2 = streamObj2.str();
std::cout << strObj2 << '\n';
The output = 12345.0000000000, which is not what I am expecting. Removing std::fixed gives output as 12345
What I require = 12345.00000
Please help me, thanks.
Thanks for the comments, because of which I found the answer. Using std::showpoint instead of std::fixed helped fulfill the requirement.
double num = 12345.0000000000001;
std::ostringstream streamObj2;
streamObj2 << std::showpoint << std::setprecision(10) << num;
std::string strObj2 = streamObj2.str();
std::cout << strObj2 << '\n';
Output: 12345.00000
I'm using visual studio 2015 to print two floating numbers:
double d1 = 1.5;
double d2 = 123456.789;
std::cout << "value1: " << d1 << std::endl;
std::cout << "value2: " << d2 << std::endl;
std::cout << "maximum number of significant decimal digits (value1): " << -std::log10(std::nextafter(d1, std::numeric_limits<double>::max()) - d1) << std::endl;
std::cout << "maximum number of significant decimal digits (value2): " << -std::log10(std::nextafter(d2, std::numeric_limits<double>::max()) - d2) << std::endl;
This prints the following:
value1: 1.5
value2: 123457
maximum number of significant decimal digits (value1): 15.6536
maximum number of significant decimal digits (value2): 10.8371
Why 123457 is printed out for the value 123456.789? Does ANSI C++ specification allow to display anything for floating numbers when std::cout is used without std::setprecision()?
The rounding off happens because of the C++ standard which can be seen by writing
std::cout<<std::cout.precision();
The output screen will show 6 which tells that the default number of significant digits which will be printed by the std::cout statement is 6. That is why it automatically rounds off the floating number to 6 digits.
What you have have pointed out is actually one of those many things that the standardization committee should consider regarding the standard iostream in C++. Such things work well when you write :-
printf ("%f\n", d2);
But not with std::cout where you need to use std::setprecision because it's formatting is similar to the use of %g instead of %f in printf. So you need to write :-
std::cout << std::setprecision(10) << "value2: " << d2 << std::endl;
But if you dont like this method & are using C++11 (& onwards) then you can also write :-
std::cout << "value2: " << std::to_string(d2) << std::endl;
This will give you the same result as printf ("%f\n", d2);.
A much better method is to cancel the rounding that occurs in std::cout by using std::fixed :-
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::fixed;
double d = 123456.789;
std::cout << d;
return 0;
}
Output :-
123456.789000
So I guess your problem is solved !!
I think the problem here is that the C++ standard is not written to be easy to read, it is written to be precise and not repeat itself. So if you look up the operator<<(double), it doesn't say anything other than "it uses num_put - because that is how the cout << some_float_value is implemented.
The default behaviour is what print("%g", value); does [table 88 in n3337 version of the C++ standard explains what the equivalence of printf and c++ formatting]. So if you want to do %.16g you need to change the precision by calling setprecision(16).
I am having trouble converting a string to a double. I am given a string with lat/long coordinates in the format of 33.9425/N 118.4081/W
I first call my function trimLastChar(std::string& input) twice, which will remove the the North, South, East, West characters and then the forward slash. This function correctly returns 33.9425 and 118.4081 respectfully as a std::string.
I am using the following code to convert my std::string to a double...however the problem is, the conversion losses precision -- I suspect it gets rounded?.
// Location In String is what trimLastChar returns
std::stringstream stream(locationInString);
std::cout << "DEBUG: before: " << locationInString << " ";
// output is a double* output = new double passed by reference to my function
stream >> output;
std::cout << output << std::endl;
The output in this case would produce:
33.9425
118.408
As you notice, the correct value should be 118.4081 but the 1 is missing...
Any ideas how to fix? Or more important, why is this happening?
The precision wasn't lost on input. It's being lost on output.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
double v = 118.4081;
cout << v << endl;
cout.precision(10);
cout << v << endl;
}
outputs:
$ g++ -Wall x.cpp && ./a.out
118.408
118.4081
$
You probably have more digits than the output shows. By default, only a small number of digits is shown, you need to use std::setprecision to see more digits. Try
std::cout << std::setprecision(10) << output << std::endl;
As I understand it the setprecision function specifies the minimal precision but when I run the following code I get only 3 numbers after the decimal point:
int main()
{
double a = 123.4567890;
double b = 123.4000000;
std::cout << std::setprecision(5) << a << std::endl; // Udesireble
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(5) << a << std::endl; // Desireble
std::cout << std::setprecision(5) << b << std::endl; // Udesireble
std::cout.unsetf(std::ios::fixed);
std::cout << std::setprecision(5) << b << std::endl; // Desireble
return 0;
}
which prints:
123.46 // Udesireble
123.45679 // Desireble
123.40000 // Udesireble
123.4 // Desireble
Is there any way I can avoid checking the number of digits after the decimal point myself in order to know whether to set fixed ?
My impression is that you will need to format to string first, and then replace trailing zeros with spaces.
For the streams, you can use two functions.
setfill(char_type c), which set the character to write, to match with the number of needed character (more information here)
There is the setw(int) function, which set the width of field of the value to display. (documentation here )
Using these functions, you may have a solution