Showing numbers in decimal not scientific notation
-
no need to use QTextStream in that case:
lineEdit->setText(lineEdit->locale().toString(1257.147862,'f'));
'f'
prevents scientific notation, see http://doc.qt.io/qt-5/qstring.html#argument-formats -
12
is not adouble
, it's anint
.12.0
is a double and12.0f
is a float.
So anyway, like I said earlier, eitherQString s; QTextStream foo(&s); foo.setRealNumberNotation(QTextStream::FixedNotation); s << 12.0 ; //assuming you still want that double and not int lineEdit -> setText(s);
or
QString s; QTextStream (&s) << QString::number(12.0, 'f'); lineEdit -> setText(s);
but then it's kinda pointless, you can just directly do
lineEdit -> setText(QString::number(12.0, 'f'));
...ugh is the sound you can make when you don't have the energy to explain something, so you just skip it altogether ;)
@Chris-Kawa said in Showing numbers in decimal not scientific notation:
QString s; QTextStream (&s) << QString::number(12.0, 'f'); lineEdit -> setText(s);
I used it. It's fine for
double
s but when I calculateint
s (e.g.,2+3
) it shows5.000000
!!
We should make it show the precision only when the result is adouble
number not anint
. -
@Chris-Kawa said in Showing numbers in decimal not scientific notation:
QString s; QTextStream (&s) << QString::number(12.0, 'f'); lineEdit -> setText(s);
I used it. It's fine for
double
s but when I calculateint
s (e.g.,2+3
) it shows5.000000
!!
We should make it show the precision only when the result is adouble
number not anint
.@tomy said in Showing numbers in decimal not scientific notation:
I used it. It's fine for doubles but when I calculate ints (e.g., 2+3) it shows 5.000000!!
We should make it show the precision only when the result is a double number not an int.You either calculate a double or an int, can't be both. Anyway, what you probably want is this:
double calculationResult = 12.0; QString displayValue = qFuzzyCompare(calculationResult, static_cast<qint64>(calculationResult)) ? QString::number(static_cast<qint64>(calculationResult)) : QString::number(calculationResult, 'f');
-
@tomy said in Showing numbers in decimal not scientific notation:
I used it. It's fine for doubles but when I calculate ints (e.g., 2+3) it shows 5.000000!!
We should make it show the precision only when the result is a double number not an int.You either calculate a double or an int, can't be both. Anyway, what you probably want is this:
double calculationResult = 12.0; QString displayValue = qFuzzyCompare(calculationResult, static_cast<qint64>(calculationResult)) ? QString::number(static_cast<qint64>(calculationResult)) : QString::number(calculationResult, 'f');
You either calculate a double or an int, can't be both.
Anyway, what you probably want is this:How can it be acceptable!? All calculator around the world do calculations on both types. Furthermore, when I write
2.3
+4.6
, it shows:6.900000
!
That is it works fine neither for ints nor for doubles.double calculationResult = 12.0; QString displayValue = qFuzzyCompare(calculationResult, static_cast<qint64>(calculationResult) ? QString::number(static_cast<qint64>(calculationResult)) : QString::number(calculationResult, 'f');
Ow my God! Isn't there any simpler way?
Please have a look at Windows built-in calculator. See this how simple and nifty shows results.
Do you say that behind that Windows' calculator there would be such a long statement just for showing numbers in decimal mode, if it were written by C++/Qt? -
You either calculate a double or an int, can't be both.
Anyway, what you probably want is this:How can it be acceptable!? All calculator around the world do calculations on both types. Furthermore, when I write
2.3
+4.6
, it shows:6.900000
!
That is it works fine neither for ints nor for doubles.double calculationResult = 12.0; QString displayValue = qFuzzyCompare(calculationResult, static_cast<qint64>(calculationResult) ? QString::number(static_cast<qint64>(calculationResult)) : QString::number(calculationResult, 'f');
Ow my God! Isn't there any simpler way?
Please have a look at Windows built-in calculator. See this how simple and nifty shows results.
Do you say that behind that Windows' calculator there would be such a long statement just for showing numbers in decimal mode, if it were written by C++/Qt?@tomy said in Showing numbers in decimal not scientific notation:
How can it be acceptable!? All calculator around the world do calculations on both types. Furthermore, when I write 2.3+4.6, it shows: 6.900000!
No they do calculation in the widest possible type they support (here it's double) and then display the result as appropriate. Further reading on implicit type promotions in c++ can be found here
Ow my God! Isn't there any simpler way?
This is a simple
if-else
statement with the notable exception that it compares floating point values as they should be compared.Do you say that behind that Windows' calculator there would be such a long statement just for showing numbers in decimal mode, if it were written by C++/Qt?
Yes, I'm sure of it. It's probably even much longer as windows is actually written in C.
-
I used this:
ss = qFuzzyCompare(expression(), static_cast<qint64>(expression()) ? QString::number(static_cast<qint64>(expression())) : QString::number(expression(), 'f')); result_box -> setText(ss);
ss
is a QString.
expression()
returns anint
ordouble
value.
result_box
is alineEdit
which shows the result of the calculations.I get this error:
C:\Users\ME\Documents\Qt\My_First_Calculator\my_first_calculator.cpp:81: error: no matching function for call to 'qFuzzyCompare(double, QString)'
QString::number(expression(), 'f'));
^ -
I used this:
ss = qFuzzyCompare(expression(), static_cast<qint64>(expression()) ? QString::number(static_cast<qint64>(expression())) : QString::number(expression(), 'f')); result_box -> setText(ss);
ss
is a QString.
expression()
returns anint
ordouble
value.
result_box
is alineEdit
which shows the result of the calculations.I get this error:
C:\Users\ME\Documents\Qt\My_First_Calculator\my_first_calculator.cpp:81: error: no matching function for call to 'qFuzzyCompare(double, QString)'
QString::number(expression(), 'f'));
^@tomy said in Showing numbers in decimal not scientific notation:
expression() returns an int or double value.
I'm pretty sure that functions returns a string, not an
int
, nor adouble
. You need to convert the string to an actual number if you want to use it as such, e.g. see here. -
@tomy said in Showing numbers in decimal not scientific notation:
expression() returns an int or double value.
I'm pretty sure that functions returns a string, not an
int
, nor adouble
. You need to convert the string to an actual number if you want to use it as such, e.g. see here.expression() returns an int or double value.
I'm pretty sure that functions returns a string, not an
int
, nor adouble
.Don't be that sure. :)
I have this method in my code:double My_First_Calculator::expression()
But I think this error is of that
ss
is aQString
. -
You either calculate a double or an int, can't be both.
Anyway, what you probably want is this:How can it be acceptable!? All calculator around the world do calculations on both types. Furthermore, when I write
2.3
+4.6
, it shows:6.900000
!
That is it works fine neither for ints nor for doubles.double calculationResult = 12.0; QString displayValue = qFuzzyCompare(calculationResult, static_cast<qint64>(calculationResult) ? QString::number(static_cast<qint64>(calculationResult)) : QString::number(calculationResult, 'f');
Ow my God! Isn't there any simpler way?
Please have a look at Windows built-in calculator. See this how simple and nifty shows results.
Do you say that behind that Windows' calculator there would be such a long statement just for showing numbers in decimal mode, if it were written by C++/Qt? -
Should I use it this way:
QString ss; ss = result_box -> locale().toString(1257.147862,'f'); QTextStream (&ss) << expression(); result_box -> setText(ss);
?
@tomy No, don't use QTextStream you don't need something like stringstream to pass numbers to string.
QString ss; ss = result_box -> locale().toString(expression(),'f'); result_box -> setText(ss);
or more concisely,
result_box->setText(result_box->locale().toString(expression(),'f'));
-
@tomy No, don't use QTextStream you don't need something like stringstream to pass numbers to string.
QString ss; ss = result_box -> locale().toString(expression(),'f'); result_box -> setText(ss);
or more concisely,
result_box->setText(result_box->locale().toString(expression(),'f'));
@VRonin said in Showing numbers in decimal not scientific notation:
@tomy No, don't use QTextStream you don't need something like stringstream to pass numbers to string.
QString ss; ss = result_box -> locale().toString(expression(),'f'); result_box -> setText(ss);
I used it.
2 + 3 = 5.000000
:( :( -
what is result_box
It's a
lineEdit
.and what is inside expression()?
It returns only a
double
value. Consider something simple like:double My_First_Calculator::expression() { double d1, d2; // these d1, d2 are gotten from input E.g. d1 = 2, d2 = 3.5 if(_ch == '+') return d1+d2; // _ch is a previously defined varible else if (_ch == '-') return d1-d2; // and so on }
-
I simplified the code as follows. This, too, has exactly that problem:
test.h
#ifndef TEST_H #define TEST_H #include <QDialog> class QLineEdit; class QPushButton; class test : public QDialog { Q_OBJECT public: test(QWidget* parent = 0); private slots: void expression(); private: QLineEdit* result_box; QPushButton* equal; QPushButton* quit; }; #endif // TEST_H
test.cpp
#include <QtWidgets> #include "test.h" test::test(QWidget* parent) : QDialog(parent) { result_box = new QLineEdit; equal = new QPushButton(tr("=")); quit = new QPushButton(tr("Close")); connect(quit, SIGNAL(clicked(bool)), this, SLOT(close())); connect(equal,SIGNAL(clicked(bool)), this, SLOT(expression())); QHBoxLayout* layout = new QHBoxLayout; layout -> addWidget(result_box); layout -> addWidget(equal); layout -> addWidget(quit); setLayout(layout); } //****************** void test::expression() { QString ss; double d = 1000000; QTextStream (&ss) << d; result_box -> setText(ss); }
And
main.cpp
#include <QApplication> #include "test.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); test t; t.show(); return app.exec(); }
Just run it and click on the
=
button. -
I simplified the code as follows. This, too, has exactly that problem:
test.h
#ifndef TEST_H #define TEST_H #include <QDialog> class QLineEdit; class QPushButton; class test : public QDialog { Q_OBJECT public: test(QWidget* parent = 0); private slots: void expression(); private: QLineEdit* result_box; QPushButton* equal; QPushButton* quit; }; #endif // TEST_H
test.cpp
#include <QtWidgets> #include "test.h" test::test(QWidget* parent) : QDialog(parent) { result_box = new QLineEdit; equal = new QPushButton(tr("=")); quit = new QPushButton(tr("Close")); connect(quit, SIGNAL(clicked(bool)), this, SLOT(close())); connect(equal,SIGNAL(clicked(bool)), this, SLOT(expression())); QHBoxLayout* layout = new QHBoxLayout; layout -> addWidget(result_box); layout -> addWidget(equal); layout -> addWidget(quit); setLayout(layout); } //****************** void test::expression() { QString ss; double d = 1000000; QTextStream (&ss) << d; result_box -> setText(ss); }
And
main.cpp
#include <QApplication> #include "test.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); test t; t.show(); return app.exec(); }
Just run it and click on the
=
button. -
I think I should write a code for it like this:
if( d is like an int number) result_box -> setText(QString::number(d , 'f', 0)); else if ( d is a double number with n numbers after point) result_box -> setText(QString::number(d , 'f', n));
-
expression() returns an int or double value.
I'm pretty sure that functions returns a string, not an
int
, nor adouble
.Don't be that sure. :)
I have this method in my code:double My_First_Calculator::expression()
But I think this error is of that
ss
is aQString
.@tomy said in Showing numbers in decimal not scientific notation:
Don't be that sure. :)
Yes! There's a typo in the code ... :)
ss = qFuzzyCompare(expression(), static_cast<qint64>(expression()) ? //< Missing a ) QString::number(static_cast<qint64>(expression())) : QString::number(expression(), 'f')); //< Extra )
What you want is to have the
if
withqFuzzyCompare
, not with the static cast. It should rather read like this:ss = qFuzzyCompare(expression(), static_cast<qint64>(expression())) ? QString::number(static_cast<qint64>(expression())) : QString::number(expression(), 'f');
If you wish you can of course use the usual
if-else
construct, not the ternary operator, so the last snippet'd be equivalent to:if (qFuzzyCompare(expression(), static_cast<qint64>(expression())) ss = QString::number(static_cast<qint64>(expression())); else ss = QString::number(expression(), 'f');
-
@tomy said in Showing numbers in decimal not scientific notation:
Don't be that sure. :)
Yes! There's a typo in the code ... :)
ss = qFuzzyCompare(expression(), static_cast<qint64>(expression()) ? //< Missing a ) QString::number(static_cast<qint64>(expression())) : QString::number(expression(), 'f')); //< Extra )
What you want is to have the
if
withqFuzzyCompare
, not with the static cast. It should rather read like this:ss = qFuzzyCompare(expression(), static_cast<qint64>(expression())) ? QString::number(static_cast<qint64>(expression())) : QString::number(expression(), 'f');
If you wish you can of course use the usual
if-else
construct, not the ternary operator, so the last snippet'd be equivalent to:if (qFuzzyCompare(expression(), static_cast<qint64>(expression())) ss = QString::number(static_cast<qint64>(expression())); else ss = QString::number(expression(), 'f');