Unsolved Cannot extract a usable double or float from std::string or qstring
-
I am reading data from a csv file into a user defined structure.
struct reqs { std::string n_name; std::string units; std::string amt_grams; std::string amt_pct; };
I the do a pushback of this structure into a std::vector of type <reqs>
std::vector<reqs> rvector;
I print this vector out using qDebug in a loop from from 0 to < rvector.size().
The numbers that are 1,000,000 or larger print in scientific notation 1e6.
When I try to do math operations on these numbers they are ALWAYS off by a factor of 1000.
I think that the exponent cannot be interpreted correctly.
I've made separate vectors for each item in the structure of type <double> and parsed through
the rvector loading each vector using this conversion to floatdouble tmpdbl = (double)atof(rvector[i].units.c_str()); req_units_vector.push_back(tmpdbl);
Still when I try to display using qDebug or std::cout I get exponents and get the same bogus division result which is off
by 1000.Any suggestions on getting ONLY doubles and no exponent representation.
-
Please provide an example.
-
Hi,
Since you have strings why not use stof ?
It has the advantage of screaming at you if there's an issue during the conversion.
-
@emp1953
@SGaist suggests usingstof
. Since you say you are storing the numbers asdouble
s notfloat
s, why not use the underlying strtod or the corresponding stod, rather than gratuitously discarding the extra precision?Note that the "exponent representation" is produced on output.
qDebug()
uses the default floating point output, which goes to exponent for small/large values, as well as limiting to 6 significant digits. You can change that to e.g. never use exponent via, say,qDebug() << QString::number(doubleNum, 'f', 12)
, https://doc.qt.io/qt-5/qstring.html#number-6. -
@JonB Agreed about the exponent representation on qDebug output. But when I put the numbers into a qtablewidget item they also are exponential. Not as easy for the laymen user to visualize.
-
@Christian-Ehrlicher Do you need example output or code?
-
We want to see what you're doing so write three lines where you parse the string and output it where you think the output is wrong.
-
@emp1953 said in Cannot extract a usable double or float from std::string or qstring:
But when I put the numbers into a qtablewidget item they also are exponential.
At some point you want to call
QString::number(doubleNum, 'f', 12)
. That could be when storing them as strings, or in aQStyledItemDelegate
for output. This is just for the issue of displaying them.