How to convert string to double ?
-
Hi all,
I am new in qt. I want convert string to double . My string is= "12345.54". When i used string.toDouble() i am getting this result = 12345.5 , i want this result=12345.54, why i can't this result ? Also when i changed string this = "12345.56" or "12345.57", i am getting this result=12345.6
Regards
-
Hi,
How do you determine that you have that ?
-
My code is below:
QString number="12345.54";
double x=number.toDouble();
qDebug()<<x;My output is= 12345.5
@Mucahit
That is just an artefact of howqDebug()
displays adouble
by default (6 digits). use QString QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' ')) const to specify more precision. -
You can use qSetRealNumberPrecision to ensure you have the precision you want to show the numbers you are using.
-
You can use qSetRealNumberPrecision to ensure you have the precision you want to show the numbers you are using.
-
@SGaist
I didn't know about your way. How do you accessqDebug()
'sQTextStream
to call yours? Are you saying you just callqDebug().qSetRealNumberPrecision()
??double testValue = 3.141592653589; qDebug() << "Some text" << testValue; qDebug() << "Some text" << qSetRealNumberPrecision(12) << testValue;
The second does not show the full extent of testValue but illustrates the use.
Note that like with any floating point number, the representation is not guaranteed as not every number can be represented in a computer.
-
double testValue = 3.141592653589; qDebug() << "Some text" << testValue; qDebug() << "Some text" << qSetRealNumberPrecision(12) << testValue;
The second does not show the full extent of testValue but illustrates the use.
Note that like with any floating point number, the representation is not guaranteed as not every number can be represented in a computer.
@SGaist
Hmm. Thank you. I find this use of C++ streams (and<<
) hard to get my head around, logically! If you can goqDebug() << value
to print out a value, I find it conceptually difficult to sendqSetRealNumberPrecision(12)
to the operator in the same way.And while we're here: does that
<< qSetRealNumberPrecision(12)
only affect items output anywhere in the same single statement? Or just the item immediately to the right of it? Or is it permanently altering the output format of theqDebug()
stream from then onward? Or...? :) -
@SGaist
Hmm. Thank you. I find this use of C++ streams (and<<
) hard to get my head around, logically! If you can goqDebug() << value
to print out a value, I find it conceptually difficult to sendqSetRealNumberPrecision(12)
to the operator in the same way.And while we're here: does that
<< qSetRealNumberPrecision(12)
only affect items output anywhere in the same single statement? Or just the item immediately to the right of it? Or is it permanently altering the output format of theqDebug()
stream from then onward? Or...? :)@JonB
It affects everything to the right, in the same statement. If my test is correct.QString testValue = "3.141592653589"; double pi = testValue.toDouble(); qDebug() << "by default" << pi; qDebug() << "in the same statement witout qSetRealNumberPrecision() " << pi << "and after setting qSetRealNumberPrecision() "<< qSetRealNumberPrecision(12) << pi << " one more " << pi; qDebug() << "after" << pi;
//output by default 3.14159 in the same statement witout qSetRealNumberPrecision() 3.14159 and after setting qSetRealNumberPrecision() 3.14159265359 one more 3.14159265359 after 3.14159
-
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.