PDA

View Full Version : How to make a lineEdit show decimal numbers instead of scientific



franky
28th January 2017, 18:52
Hi all,

This is a section of my app which is a calculator in Qt:


QString ss;
QTextStream (&ss) << expression(); // expression() return a double value
result_box -> setText(ss); // result_box is a lineEdit


When I type 10^6 and the function expression() returns that value, in result_box the scientific notation of it will be shown, 1e+06!

How to do to make the app show the result in decimal, 1000000 rather than that scientific notation please?

ChrisW67
29th January 2017, 09:07
result_box->setText( QString::number(expression(), 'f', 0) );

Gokulnathvc
12th September 2017, 14:37
You can use like the following::


result_box->setText( QString::number(expression(), 'd', 0) );

Ginsengelf
14th September 2017, 09:05
Using 'd' is not explicitely defined. It works only because QString::number() defaults to 'f' if any undefined character is used.
As you can see from this small example, 'd', 'q', 'a', '#' all produce the same output.


#include <QtCore/QCoreApplication>

#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

double dValue = 123e15;

qDebug () << 'g' << QString::number (dValue, 'g', 0);
qDebug () << 'f' << QString::number (dValue, 'f', 0);

qDebug () << 'd' << QString::number (dValue, 'd', 0);
qDebug () << 'a' << QString::number (dValue, 'a', 0);
qDebug () << 'q' << QString::number (dValue, 'q', 0);
qDebug () << '#' << QString::number (dValue, '#', 0);

return a.exec();
}

Output:
g "1e+17"
f "123000000000000000"
d "123000000000000000"
a "123000000000000000"
q "123000000000000000"
# "123000000000000000"

But please do not use undefined values. They might get meaningful in the future, and you would get unexpected results. As ChrisW67 already wrote using 'f' is the way to go.

Ginsengelf