Unsolved how to deal with numbers containing a comma?
-
Hi, as a follow up on another topic a new issue arose.
https://forum.qt.io/topic/108209/qlineedit-autofill/13.the following operation returns sometimes fractal numbers containing a comma.
...... (baseNumber/1000000.0);
i can't do further operations depending on the results of the code above.
"0,00012", does not further compute
"1e-05", does further compute.do you need to use signed variables?
special measurements when handling big numbers?how to turn "1e-05" this into "0,0001" again?
-
@Natural_Bugger
Have you looked at https://doc.qt.io/qt-5/qstring.html#argument-formats ? I suspect you wantf
format, the default is probablyg
? And if you have commas in your input/output, look at the locale you are using. -
thnx for your pront reply:
the main problem is that i can do further computing on numbers containing a comma, the results of the equation above, before putting it back into a a QLineEdit form field.
"inf" shows up in the QlineEdit form field. -
@Natural_Bugger
Yes, you need to convert (comma) string representation back to the actual number (float/double) before you can do computations on it. -
this is what is what i do.
float solution = 0.0; QString one = ui->lineEdit_1->text(); float floatOne = one.toFloat(); QString two = ui->lineEdit_2->text(); float floatTwo = twor.toFloat(); solution = 1/(2 * M_PI * floatOne * floatTwo); // it stalls here. ui->lineEdit_3->->setText(QString::number(solution, 'f', 10));
this occurs after operation from the other thread.
-
@Natural_Bugger said in how to deal with numbers containing a comma?:
this occurs after operation from the other thread
What does this other thread do?
Does it access UI? -
@Natural_Bugger
Apart from what @jsulm asks you......
QString::toFloat()
won't tell you if it succeeds. You need to pass in the&ok
parameter and check that, see the docs. Plus docs tell youThe string conversion will always happen in the 'C' locale. For locale dependent conversion use
QLocale::toFloat()
For historical reasons, this function does not handle thousands group separators. If you need to convert such numbers, use
QLocale::toFloat()
. -
Hi,
Why not use QSpinBox and/or QDoubleSpinBox ?
That way you don't need to go back and forth with strings.
-