How to convert string to double ?
-
Thank you so much. I never thought the mistake was caused by qDebug(), it really works. So how can i print this value on any label ? i want thousand separator. For example:
QString number="12345.54";
double x=number.toDouble();
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<x; //this output correctui->label_any->setText(QString("%L1").arg(x));
I still have the same problem. I want it to write like this at label = "12,345.54" . How can I do that ?
-
Thank you so much. I never thought the mistake was caused by qDebug(), it really works. So how can i print this value on any label ? i want thousand separator. For example:
QString number="12345.54";
double x=number.toDouble();
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<x; //this output correctui->label_any->setText(QString("%L1").arg(x));
I still have the same problem. I want it to write like this at label = "12,345.54" . How can I do that ?
@Mucahit
I already told you in the first place! See the link in my first reply to your question.
That overload also offers to deal with locale, for your desired thousand-separator.
You can use that withqDebug()
,QLabel::setText()
or anywhere else a string is wanted. -
No, I guess I could not explain exactly. The problem is not printing on labels right now.
QString number="12345.54";
double x=number.toDouble();
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<x; //this output correctbut this output is not correct :
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<QString("%L1").arg(x); //this output not correct
This is the exit I got : some text 12,345.5
This is the exit I want: some text 12,345.54 -
No, I guess I could not explain exactly. The problem is not printing on labels right now.
QString number="12345.54";
double x=number.toDouble();
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<x; //this output correctbut this output is not correct :
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<QString("%L1").arg(x); //this output not correct
This is the exit I got : some text 12,345.5
This is the exit I want: some text 12,345.54This post is deleted! -
No, I guess I could not explain exactly. The problem is not printing on labels right now.
QString number="12345.54";
double x=number.toDouble();
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<x; //this output correctbut this output is not correct :
qDebug()<<"result"<<qSetRealNumberPrecision(12)<<QString("%L1").arg(x); //this output not correct
This is the exit I got : some text 12,345.5
This is the exit I want: some text 12,345.54@Mucahit
You are simply mixing upqSetRealNumberPrecision()
withQString.arg()
and trying to combine them in a way which will never work.Please get rid of
qSetRealNumberPrecision()
, pretend you had never heard of it, it only affectsqDebug()
, you won't be able to use for setting your label text. Just use the function I linked you, nothing else, look at all of its arguments, there is even an example there. -
-
Yes I looked but I couldn't because I'm new to the qt. I know you're mad at me but I couldn't look at the documents. Can you give me a sample code? How can I divide the string number "12345.54" by thousands ? I want to get this result = 12,345.54
int main(int argc, char *argv[]) { QApplication a(argc, argv); auto *l = new QLabel(); const double d{12345.54}; const QString str = QLocale(QLocale::German).toString(d, 'g',12); qDebug() << str; l->resize(200,50); l->show(); l->setText(str); return a.exec(); }
-
Yes I looked but I couldn't because I'm new to the qt. I know you're mad at me but I couldn't look at the documents. Can you give me a sample code? How can I divide the string number "12345.54" by thousands ? I want to get this result = 12,345.54
@Mucahit said in How to convert string to double ?:
I know you're mad at me
I was never mad at you! :)
Unfortunately for your usage beyond just with
qDebug()
(because you want to be able to get a string to put in a widget),qSetRealNumberPrecision()
was a red herring (means: not relevant here), and was confusing you. A string solution, likeQString::arg()
or @J-Hilk'sQLocale::toString()
, which allows you to request the number formatted to a locale (thousand separator character, decimal point character) and to a specified precision (more than the default 6 digits) is what you want.