+ 2
What's wrong with to_string
Hi Am i missing something here in below code? I am always getting value truncated due to to_string. What to do to get exact value from double to string in to_string? https://sololearn.com/compiler-playground/crQgPYAU0HfZ/?ref=app
8 Answers
+ 4
Ketan Lalcheta if it is important to echo back the user float value exactly as entered, then it might work better to receive the entry as a string. Convert the string to float or double or long double for calculation purposes. Use the string for display. (Then you can count characters up to the decimal in order to determine the input precision).
+ 4
I looked it up and learned that to_string() has a fixed limit of 6 decimal places. You cannot adjust it.
You can use stringstream with iomanip and filter the value through setprecision().
+ 3
Floating point precision is always an issue with programming. This particular function, to_string(), is actually documented that it may have precision issues, which you've discovered. In practice, most programs do not need that level of precision. If they did not have restrictions on precision, then you would see a lot of numbers like 1.000000000000001 showing up.
There is a newer command, std::format() which can be used to specify precision, but that doesn't work on the SoloLearn compiler. Apparently SoloLearn is using an older standard library for C++. You can read about it here:
https://en.cppreference.com/w/cpp/utility/format/spec
+ 2
Jerry Hobby thanks but unfortunately our standard tool is still Visual Studio 2019 and cannot upgrade it for this issue
+ 2
Sounds good Brian
+ 1
Brian I also used set precision when I was supposed to use truncated value.
Fixed and set precision is used by me .
I observed this issue when it was not supposed to have any precision and should show user entered value.
In that case , I was using to_string. Now if I want to use also, what should be the precision value to set as I don't know what user has entered
+ 1
Hello,
You are experiencing value truncation when converting a double to a string using std::to_string, it could be because std::to_string formats the number to a fixed number of decimal places, which might not represent the full precision of your double.
Here are a few things you can try:
Use std::ostringstream: Instead of std::to_string, you can use a std::ostringstream to control the formatting and precision: #include <sstream>
#include <iomanip>
#include <string>
std::string https://www.my-fordbenefits.com doubleToString(double value, int precision = 15) {
std::ostringstream out;
out << std::fixed << std::setprecision(precision) << value;
return out.str();
}
This way, you can specify how many decimal places you want, and you can avoid truncation.
Check the Value of the Double: Ensure that the value of the double you're trying to convert is what you expect. Sometimes, it may appear truncated because of the limitations of floating-point representation.
Adjust Precision: If you're using std::to_string, remember that it usually provides 6 decimal places by default. If you need more precision, use the first approach. #include <iostream>
#include <sstream>
#include <iomanip>
int main() {
double value = 12345.67890123456789;
std::string strValue = doubleToString(value, 15);
std::cout << strValue << std::endl; // Should output full precision
return 0;
}
0
How about lexical_cast ? Does it help or only option is to have string only (as suggested by Brian ) and use it as per double or float when needed.