I have a string that looks like this:
"0.4794255386042030002732879352156"
which is approximately the sin(0.5). I would like to format the string to look a much nicer
"4.794255386042e-1"
How can I achieve this? Remember I am dealing with strings and not numbers (float, double). Also I need to round to keep the number as accurate as possible, I can't just truncate. If I need to convert to a different data type I would prefer a long double because a regular double doesn't have enough precision. I'd like at least 12 decimal digits before rounding. Perhaps there is a simple sprintf() conversion I could do.
Something like this:
#include<iostream>
using namespace std;
int main()
{
char *s = "0.4794255386042030002732879352156";
double d;
sscanf(s,"%lf",&d);
printf("%.12e\n",d);
return EXIT_SUCCESS;
}
Output:
# g++ a.cpp && ./a.out
4.794255386042e-01
Are you looking for something like this?
Here is a sample:
// modify basefield
#include <iostream>
#include <sstream>
using namespace std;
int main () {
std::string numbers("0.4794255386042030002732879352156");
std::stringstream stream;
stream << numbers;
double number_fmt;
stream >> number_fmt;
cout.precision(30);
cout << number_fmt << endl;
cout.precision(5);
cout << scientific << number_fmt << endl;
return 0;
}
Output:
0.479425538604203005377257795772
4.79426e-01
In highly portable C the working example below outputs:
result is 4.794255386042E-01
#include <stdio.h>
int main()
{
char *str = "0.4794255386042030002732879352156";
double f;
char newstr [50];
// convert string in `str` to float
sscanf (str, "%le", &f);
sprintf (newstr, "%.12E", f);
printf ("result is %s\n", newstr);
return 0;
}
Looking at the strings in your question, it would seem you are using base-10 logarithms. In that case wouldn't it be relatively easy to just count the leading or trailing zeros and put that in an exponent, by scanning the strings directly?
Maybe i'm missing something..
An IEEE 754 64 bit float (i.e. double precision), is good for 15 decimal significant figures, so you should have no problem converting to double. You are more likely to run into the problem of getting the format specifier to display all available digits. Although from the examples posted, this seems not to be the case.
Convert to long double using sscanf(), then use sprintf() to print it back out as a string, using the scientific formatter:
long double x;
char num[64];
if(sscanf(string, "%Lf", &x) == 1)
sprintf(num, "%.12Le", x);
Not sure if even long double actually gives you 12 significant digits, though. On my system, this generates "4.79425538604e-01".
There is no standard function in either C or C++ to do this. The normal approach is either to convert to a number (and then use standard functions to format the number) or write your own custom output function.
#include <iostream>
using namespace std;
...
double dd = strtod( str );
cout << scientific << dd << endl;
Depending on how many decimal places you want (12 in this case) you could do something like this:
int main() {
char buff[500];
string x = "0.4794255386042030002732879352156";
double f = atof(x.c_str());
sprintf(buff,"%.12fe-1",f*10);
cout<<buff<<endl;
return 0;
}
Result:
---------- Capture Output ----------
"c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
4.794255386042e-1
Terminated with exit code 0.
Related
Updated:
I have been trying all methods I could find in stackoverflow, and still could not find a solution.
My point is, I have a string "23.46" and would like to transfer it to double or float anyway. This is in order to be used in another library.
But no matter how I trancate,floor,*100,add,round, it always gives me 24.4600000001 or something like this.
I know there are some precision issue while transfer. But I do need a way to give me a number that double d = 24.46 precisely.
==============================================================================
I have many string values and some of them are double with converted precision as below:
char pt[100];
sprintf(pt, "%.2lf", i);
return string(pt);
Now on the other side of the code, I need to convert the strings back to double, but I tried strtod and atof with precision loss.
My questions are:
what is the good way to check if a string could be a double?
how to convert string back to double with given precision? I only need it to %.2lf be like:
0.21, 35.45, ...
Thanks so much!
Given that you say that using std::strtod is not giving you a solution to the problem, you can use stringstreams to parse strings as doubles, you can also use its flags to assert if the contents of the string are convertible.
Here is an example with some conversions back and forth, and with checks to see if the whole string (not just some digits in it), is parseable as double:
Live demo
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::string str = "23.4k7"; //string with a character in the middle
std::istringstream ss(str); //put string in a stream
double num;
ss >> num; //convert string to double
if(!ss.fail() && ss.eof()) { //check if the whole string is parseable
std::cout << "is parseable" << std::endl;
}
else {
std::cout << "is not parseable";
return EXIT_FAILURE;
}
std::stringstream to_num;
to_num << std::fixed << std::setprecision(2) << num; //double to string 2 decimal places
std::cout << to_num.str();
to_num >> num; //final conversion to double
}
Since the string has an alphabetic character in it, this will output:
is not parseable
But if you use a valid number it will output the converted value:
string str:
234.2345
Output:
is parseable
234.23
Note that you could use
Live demo
if(ss >> num)
std::cout << "is parseable";
This, however, has a weakness, it will still parse if you have for instance 123.45rt56, 123.45 will be parsed, the rest will be discarded, the way it is in the sample code, if the string has any character, it will return an error. You can choose the more appropriate way for your needs.
How do you convert a float to a string in C++ while specifying the precision & number of decimal digits?
For example: 3.14159265359 -> "3.14"
A typical way would be to use stringstream:
#include <iomanip>
#include <sstream>
double pi = 3.14159265359;
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << pi;
std::string s = stream.str();
See fixed
Use fixed floating-point notation
Sets the floatfield format flag for the str stream to fixed.
When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
and setprecision.
For conversions of technical purpose, like storing data in XML or JSON file, C++17 defines to_chars family of functions.
Assuming a compliant compiler (which we lack at the time of writing),
something like this can be considered:
#include <array>
#include <charconv>
double pi = 3.14159265359;
std::array<char, 128> buffer;
auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), pi,
std::chars_format::fixed, 2);
if (ec == std::errc{}) {
std::string s(buffer.data(), ptr);
// ....
}
else {
// error handling
}
The customary method for doing this sort of thing is to "print to string". In C++ that means using std::stringstream something like:
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << number;
std::string mystring = ss.str();
You can use C++20 std::format:
#include <format>
int main() {
std::string s = std::format("{:.2f}", 3.14159265359); // s == "3.14"
}
or the fmt::format function from the {fmt} library, std::format is based on (godbolt):
#include <fmt/core.h>
int main() {
std::string s = fmt::format("{:.2f}", 3.14159265359); // s == "3.14"
}
where 2 is a precision.
It is not only shorter than using iostreams or sprintf but also significantly faster and is not affected by the locale.
Another option is snprintf:
double pi = 3.1415926;
std::string s(16, '\0');
auto written = std::snprintf(&s[0], s.size(), "%.2f", pi);
s.resize(written);
Demo. Error handling should be added, i.e. checking for written < 0.
Here a solution using only std. However, note that this only rounds down.
float number = 3.14159;
std::string num_text = std::to_string(number);
std::string rounded = num_text.substr(0, num_text.find(".")+3);
For rounded it yields:
3.14
The code converts the whole float to string, but cuts all characters 2 chars after the "."
Here I am providing a negative example where your want to avoid when converting floating number to strings.
float num=99.463;
float tmp1=round(num*1000);
float tmp2=tmp1/1000;
cout << tmp1 << " " << tmp2 << " " << to_string(tmp2) << endl;
You get
99463 99.463 99.462997
Note: the num variable can be any value close to 99.463, you will get the same print out. The point is to avoid the convenient c++11 "to_string" function. It took me a while to get out this trap. The best way is the stringstream and sprintf methods (C language). C++11 or newer should provided a second parameter as the number of digits after the floating point to show. Right now the default is 6. I am positing this so that others won't wast time on this subject.
I wrote my first version, please let me know if you find any bug that needs to be fixed. You can control the exact behavior with the iomanipulator. My function is for showing the number of digits after the decimal point.
string ftos(float f, int nd) {
ostringstream ostr;
int tens = stoi("1" + string(nd, '0'));
ostr << round(f*tens)/tens;
return ostr.str();
}
How do you convert a float to a string in C++ while specifying the precision & number of decimal digits?
For example: 3.14159265359 -> "3.14"
A typical way would be to use stringstream:
#include <iomanip>
#include <sstream>
double pi = 3.14159265359;
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << pi;
std::string s = stream.str();
See fixed
Use fixed floating-point notation
Sets the floatfield format flag for the str stream to fixed.
When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
and setprecision.
For conversions of technical purpose, like storing data in XML or JSON file, C++17 defines to_chars family of functions.
Assuming a compliant compiler (which we lack at the time of writing),
something like this can be considered:
#include <array>
#include <charconv>
double pi = 3.14159265359;
std::array<char, 128> buffer;
auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), pi,
std::chars_format::fixed, 2);
if (ec == std::errc{}) {
std::string s(buffer.data(), ptr);
// ....
}
else {
// error handling
}
The customary method for doing this sort of thing is to "print to string". In C++ that means using std::stringstream something like:
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << number;
std::string mystring = ss.str();
You can use C++20 std::format:
#include <format>
int main() {
std::string s = std::format("{:.2f}", 3.14159265359); // s == "3.14"
}
or the fmt::format function from the {fmt} library, std::format is based on (godbolt):
#include <fmt/core.h>
int main() {
std::string s = fmt::format("{:.2f}", 3.14159265359); // s == "3.14"
}
where 2 is a precision.
It is not only shorter than using iostreams or sprintf but also significantly faster and is not affected by the locale.
Another option is snprintf:
double pi = 3.1415926;
std::string s(16, '\0');
auto written = std::snprintf(&s[0], s.size(), "%.2f", pi);
s.resize(written);
Demo. Error handling should be added, i.e. checking for written < 0.
Here a solution using only std. However, note that this only rounds down.
float number = 3.14159;
std::string num_text = std::to_string(number);
std::string rounded = num_text.substr(0, num_text.find(".")+3);
For rounded it yields:
3.14
The code converts the whole float to string, but cuts all characters 2 chars after the "."
Here I am providing a negative example where your want to avoid when converting floating number to strings.
float num=99.463;
float tmp1=round(num*1000);
float tmp2=tmp1/1000;
cout << tmp1 << " " << tmp2 << " " << to_string(tmp2) << endl;
You get
99463 99.463 99.462997
Note: the num variable can be any value close to 99.463, you will get the same print out. The point is to avoid the convenient c++11 "to_string" function. It took me a while to get out this trap. The best way is the stringstream and sprintf methods (C language). C++11 or newer should provided a second parameter as the number of digits after the floating point to show. Right now the default is 6. I am positing this so that others won't wast time on this subject.
I wrote my first version, please let me know if you find any bug that needs to be fixed. You can control the exact behavior with the iomanipulator. My function is for showing the number of digits after the decimal point.
string ftos(float f, int nd) {
ostringstream ostr;
int tens = stoi("1" + string(nd, '0'));
ostr << round(f*tens)/tens;
return ostr.str();
}
I have been trying to convert a double into string without the C++11 (ToString) but it only accepts the number of decimals to be 5 as default. How can I change that?
The command is:
string a = static_cast<ostringstream*>( &(ostringstream()<<digits) )->str();
but it keeps 5 decimals while I want to create a string which has all the decimals (e.g. 100)
I know that, that many decimals dont matter. This is one point of the exercise I was doing.
Any suggestions?
Thank you very much for your time
Cheers!
Use IO manipulators setprecision here on std::cout but works on stringstream:
// setprecision example from cplusplus.com
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::fixed << std::setprecision(9) << f << '\n';
return 0;
}
By the way, no double will have 100 meaningfull digit, it's 15 or 17, I forgot exactly how many.
EDIT: I forgot, if you can use C++11... You can (and should) use to_string
#include <string>
// ....
std::string f_str = std::to_string(f);
This worked for me finally
digits = 1.12345123451234;
char buff[100];
sprintf(buff, "%1.14f", digits);
string a(buff);
Thanks for checking it in any case
Cheers
The problem is that I want to output Mathematica compatible floating point numbers. The only difference with the standard IOStream or printf output format is that the exponential e is replaced by *^:
Standard C/C++ output format: 1.23e-4
Mathematica format: 1.23*^-4
Is there a way to manipulate streams to achieve this effect? My original idea was just to use istringstream and dump it to a string and then replace all the e's. I would also be okay if someone posted code to parse through the bits of the floating point number and output it directly (i.e. a printf("%e") replacement).
Your idea should work out easily:
std::string FloatToStringMathematica(float x) {
std::stringstream a;
a << x;
std::string u = a.str();
size_t pos = u.find("e");
if(pos != std::string::npos) {
u.replace(p, 1, "*^");
}
return u;
}
printf with %f shouldn't use scientific notation at all. Is there some reason you must use this notation and not simply output a long enough string to fit all significant figures?
This doesn't use streams as the OP requested, nonetheless using frexp() works for me:
#include <cmath>
#include <limits>
#include <iostream>
using namespace std;
// Print x for import to Mathematica
void cout_print(double x)
{
int exp;
cout << frexp( x, &exp );
if( exp ) cout << "*2^" << exp;
}
int main()
{
cout.precision(numeric_limits<double>::digits10);
cout_print(3.25); // 0.8125*2^2
}
This just expresses the number using a base-2 exponent instead of 10.