string to double or float cuts off decimal points [duplicate]
This question already has an answer here:
How do I print a double value with full precision using cout?
11 answers
I don't know what I am doing wrong but I want to convert a string to a double, using std::stod, but it chops of the decimal points
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << val << 'n';
}
this program produces: 321415. The stof also produces the same result.
My locale is listed below if it helps.
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
c++
marked as duplicate by gre_gor, Jean-François Fabre, rsjaffe, TylerH, jww Nov 24 '18 at 11:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
How do I print a double value with full precision using cout?
11 answers
I don't know what I am doing wrong but I want to convert a string to a double, using std::stod, but it chops of the decimal points
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << val << 'n';
}
this program produces: 321415. The stof also produces the same result.
My locale is listed below if it helps.
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
c++
marked as duplicate by gre_gor, Jean-François Fabre, rsjaffe, TylerH, jww Nov 24 '18 at 11:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Change the precision to try to see if there is something after the decimal point `std::cout << std::setprecision(10) << val << std::endl;
– Matthieu Brucher
Nov 23 '18 at 19:48
Yes, sir, there is. Should not the default output for double produce a few decimal points ? this is very surprising default behavior.
– Ring Zero.
Nov 23 '18 at 19:53
1
The default precision is to output 6 digits, not the scientific format: en.cppreference.com/w/cpp/io/manip/setprecision
– Matthieu Brucher
Nov 23 '18 at 19:57
The precision fix works but I wonder why its needed. I tried making val a double instead of auto but that didnt have any effect.
– Nirvedh Meshram
Nov 23 '18 at 19:59
Doesn't change, the default precision is 6 digits. Nothing to do with where it is.
– Matthieu Brucher
Nov 23 '18 at 20:07
|
show 1 more comment
This question already has an answer here:
How do I print a double value with full precision using cout?
11 answers
I don't know what I am doing wrong but I want to convert a string to a double, using std::stod, but it chops of the decimal points
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << val << 'n';
}
this program produces: 321415. The stof also produces the same result.
My locale is listed below if it helps.
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
c++
This question already has an answer here:
How do I print a double value with full precision using cout?
11 answers
I don't know what I am doing wrong but I want to convert a string to a double, using std::stod, but it chops of the decimal points
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << val << 'n';
}
this program produces: 321415. The stof also produces the same result.
My locale is listed below if it helps.
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
This question already has an answer here:
How do I print a double value with full precision using cout?
11 answers
c++
c++
asked Nov 23 '18 at 19:46
Ring Zero.Ring Zero.
12710
12710
marked as duplicate by gre_gor, Jean-François Fabre, rsjaffe, TylerH, jww Nov 24 '18 at 11:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by gre_gor, Jean-François Fabre, rsjaffe, TylerH, jww Nov 24 '18 at 11:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Change the precision to try to see if there is something after the decimal point `std::cout << std::setprecision(10) << val << std::endl;
– Matthieu Brucher
Nov 23 '18 at 19:48
Yes, sir, there is. Should not the default output for double produce a few decimal points ? this is very surprising default behavior.
– Ring Zero.
Nov 23 '18 at 19:53
1
The default precision is to output 6 digits, not the scientific format: en.cppreference.com/w/cpp/io/manip/setprecision
– Matthieu Brucher
Nov 23 '18 at 19:57
The precision fix works but I wonder why its needed. I tried making val a double instead of auto but that didnt have any effect.
– Nirvedh Meshram
Nov 23 '18 at 19:59
Doesn't change, the default precision is 6 digits. Nothing to do with where it is.
– Matthieu Brucher
Nov 23 '18 at 20:07
|
show 1 more comment
1
Change the precision to try to see if there is something after the decimal point `std::cout << std::setprecision(10) << val << std::endl;
– Matthieu Brucher
Nov 23 '18 at 19:48
Yes, sir, there is. Should not the default output for double produce a few decimal points ? this is very surprising default behavior.
– Ring Zero.
Nov 23 '18 at 19:53
1
The default precision is to output 6 digits, not the scientific format: en.cppreference.com/w/cpp/io/manip/setprecision
– Matthieu Brucher
Nov 23 '18 at 19:57
The precision fix works but I wonder why its needed. I tried making val a double instead of auto but that didnt have any effect.
– Nirvedh Meshram
Nov 23 '18 at 19:59
Doesn't change, the default precision is 6 digits. Nothing to do with where it is.
– Matthieu Brucher
Nov 23 '18 at 20:07
1
1
Change the precision to try to see if there is something after the decimal point `std::cout << std::setprecision(10) << val << std::endl;
– Matthieu Brucher
Nov 23 '18 at 19:48
Change the precision to try to see if there is something after the decimal point `std::cout << std::setprecision(10) << val << std::endl;
– Matthieu Brucher
Nov 23 '18 at 19:48
Yes, sir, there is. Should not the default output for double produce a few decimal points ? this is very surprising default behavior.
– Ring Zero.
Nov 23 '18 at 19:53
Yes, sir, there is. Should not the default output for double produce a few decimal points ? this is very surprising default behavior.
– Ring Zero.
Nov 23 '18 at 19:53
1
1
The default precision is to output 6 digits, not the scientific format: en.cppreference.com/w/cpp/io/manip/setprecision
– Matthieu Brucher
Nov 23 '18 at 19:57
The default precision is to output 6 digits, not the scientific format: en.cppreference.com/w/cpp/io/manip/setprecision
– Matthieu Brucher
Nov 23 '18 at 19:57
The precision fix works but I wonder why its needed. I tried making val a double instead of auto but that didnt have any effect.
– Nirvedh Meshram
Nov 23 '18 at 19:59
The precision fix works but I wonder why its needed. I tried making val a double instead of auto but that didnt have any effect.
– Nirvedh Meshram
Nov 23 '18 at 19:59
Doesn't change, the default precision is 6 digits. Nothing to do with where it is.
– Matthieu Brucher
Nov 23 '18 at 20:07
Doesn't change, the default precision is 6 digits. Nothing to do with where it is.
– Matthieu Brucher
Nov 23 '18 at 20:07
|
show 1 more comment
1 Answer
1
active
oldest
votes
You need to alter the precision. std::setprecision and std::fixed are what you are looking for. You have to include iomanip
:
#include <iostream>
#include <string>
#include <iomanip> // <- include this
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << std::setprecision(5) << std::fixed << val << std::endl;
return 0;
}
Output:
321415.13420
See this answer for more on the topic.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to alter the precision. std::setprecision and std::fixed are what you are looking for. You have to include iomanip
:
#include <iostream>
#include <string>
#include <iomanip> // <- include this
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << std::setprecision(5) << std::fixed << val << std::endl;
return 0;
}
Output:
321415.13420
See this answer for more on the topic.
add a comment |
You need to alter the precision. std::setprecision and std::fixed are what you are looking for. You have to include iomanip
:
#include <iostream>
#include <string>
#include <iomanip> // <- include this
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << std::setprecision(5) << std::fixed << val << std::endl;
return 0;
}
Output:
321415.13420
See this answer for more on the topic.
add a comment |
You need to alter the precision. std::setprecision and std::fixed are what you are looking for. You have to include iomanip
:
#include <iostream>
#include <string>
#include <iomanip> // <- include this
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << std::setprecision(5) << std::fixed << val << std::endl;
return 0;
}
Output:
321415.13420
See this answer for more on the topic.
You need to alter the precision. std::setprecision and std::fixed are what you are looking for. You have to include iomanip
:
#include <iostream>
#include <string>
#include <iomanip> // <- include this
int main()
{
auto value = std::string("321415.1342");
auto val = std::stod(value, 0);
std::cout << std::setprecision(5) << std::fixed << val << std::endl;
return 0;
}
Output:
321415.13420
See this answer for more on the topic.
edited Nov 23 '18 at 20:03
answered Nov 23 '18 at 19:57
AyxanAyxan
1,611316
1,611316
add a comment |
add a comment |
1
Change the precision to try to see if there is something after the decimal point `std::cout << std::setprecision(10) << val << std::endl;
– Matthieu Brucher
Nov 23 '18 at 19:48
Yes, sir, there is. Should not the default output for double produce a few decimal points ? this is very surprising default behavior.
– Ring Zero.
Nov 23 '18 at 19:53
1
The default precision is to output 6 digits, not the scientific format: en.cppreference.com/w/cpp/io/manip/setprecision
– Matthieu Brucher
Nov 23 '18 at 19:57
The precision fix works but I wonder why its needed. I tried making val a double instead of auto but that didnt have any effect.
– Nirvedh Meshram
Nov 23 '18 at 19:59
Doesn't change, the default precision is 6 digits. Nothing to do with where it is.
– Matthieu Brucher
Nov 23 '18 at 20:07