Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Showing numbers in decimal not scientific notation
Forum Updated to NodeBB v4.3 + New Features

Showing numbers in decimal not scientific notation

Scheduled Pinned Locked Moved Unsolved General and Desktop
41 Posts 9 Posters 31.6k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T tomy
    28 Jan 2017, 14:06

    @Chris-Kawa
    I run it in standard mode (ctrl + R).

    C Offline
    C Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on 28 Jan 2017, 14:31 last edited by
    #8

    @tomy said in Showing numbers in decimal not scientific notation:

    I run it in standard mode (ctrl + R).

    That's not what I... ugh, nevermind.
    So you say you're using QTextStream. Ok, so you have a code similar to this right?

    QTextStream foo = ...;
    double bar = ...;
    foo << bar;
    

    If that's the case then you can do it in a couple of ways:
    Switch the stream serialization mode for floating numbers to fixed notation:

    QTextStream foo = ...;
    foo.setRealNumberNotation(QTextStream::FixedNotation);
    double bar = ...;
    foo << bar;
    

    or pre-format the number the way you want it and pass the resulting string to the stream:

    QTextStream foo = ...;
    double bar = ...;
    foo << QString::number(bar, 'f');
    
    T 1 Reply Last reply 28 Jan 2017, 15:18
    3
    • C Chris Kawa
      28 Jan 2017, 14:31

      @tomy said in Showing numbers in decimal not scientific notation:

      I run it in standard mode (ctrl + R).

      That's not what I... ugh, nevermind.
      So you say you're using QTextStream. Ok, so you have a code similar to this right?

      QTextStream foo = ...;
      double bar = ...;
      foo << bar;
      

      If that's the case then you can do it in a couple of ways:
      Switch the stream serialization mode for floating numbers to fixed notation:

      QTextStream foo = ...;
      foo.setRealNumberNotation(QTextStream::FixedNotation);
      double bar = ...;
      foo << bar;
      

      or pre-format the number the way you want it and pass the resulting string to the stream:

      QTextStream foo = ...;
      double bar = ...;
      foo << QString::number(bar, 'f');
      
      T Offline
      T Offline
      tomy
      wrote on 28 Jan 2017, 15:18 last edited by tomy
      #9

      @Chris-Kawa
      What is ...ugh?! I want to learn it! :)

      My code is like this:

      QString s;
      QTextStream (&s) << 12 ;
      lineEdit -> setText(s); // It outputs 12 
      s.clear();
      
      QTextStream (&s) << "ABC";
      lineEdit -> setText(s); // This time it outputs ABC
      
      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 28 Jan 2017, 15:34 last edited by
        #10

        12 is not a double, it's an int. 12.0 is a double and 12.0f is a float.
        So anyway, like I said earlier, either

        QString 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 ;)

        T 1 Reply Last reply 28 Jan 2017, 15:56
        2
        • V Offline
          V Offline
          VRonin
          wrote on 28 Jan 2017, 15:34 last edited by VRonin
          #11

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          T 1 Reply Last reply 28 Jan 2017, 15:42
          0
          • V VRonin
            28 Jan 2017, 15:34

            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

            T Offline
            T Offline
            tomy
            wrote on 28 Jan 2017, 15:42 last edited by
            #12

            @VRonin

            lineEdit->setText(lineEdit->locale().toString(1257.147862,'f'));

            There is no s in your lineEdit!

            lineEdit -> setText( // here s should be put!)
            
            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 28 Jan 2017, 15:51 last edited by
              #13

              ok, I guess...

              QString s;
              s=lineEdit->locale().toString(1257.147862,'f');
              lineEdit -> setText(s);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • C Chris Kawa
                28 Jan 2017, 15:34

                12 is not a double, it's an int. 12.0 is a double and 12.0f is a float.
                So anyway, like I said earlier, either

                QString 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 ;)

                T Offline
                T Offline
                tomy
                wrote on 28 Jan 2017, 15:56 last edited by tomy
                #14

                @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 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.

                K 1 Reply Last reply 28 Jan 2017, 16:15
                0
                • T tomy
                  28 Jan 2017, 15:56

                  @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 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.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 28 Jan 2017, 16:15 last edited by kshegunov
                  #15

                  @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');
                  

                  Read and abide by the Qt Code of Conduct

                  T 1 Reply Last reply 28 Jan 2017, 16:35
                  1
                  • K kshegunov
                    28 Jan 2017, 16:15

                    @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');
                    
                    T Offline
                    T Offline
                    tomy
                    wrote on 28 Jan 2017, 16:35 last edited by tomy
                    #16

                    @kshegunov

                    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?

                    K V 2 Replies Last reply 28 Jan 2017, 16:40
                    0
                    • T tomy
                      28 Jan 2017, 16:35

                      @kshegunov

                      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?

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 28 Jan 2017, 16:40 last edited by kshegunov
                      #17

                      @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.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • T Offline
                        T Offline
                        tomy
                        wrote on 28 Jan 2017, 16:49 last edited by tomy
                        #18

                        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 an int or double value.
                        result_box is a lineEdit 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'));
                        ^

                        K 1 Reply Last reply 28 Jan 2017, 16:52
                        0
                        • T tomy
                          28 Jan 2017, 16:49

                          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 an int or double value.
                          result_box is a lineEdit 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'));
                          ^

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 28 Jan 2017, 16:52 last edited by
                          #19

                          @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 a double. You need to convert the string to an actual number if you want to use it as such, e.g. see here.

                          Read and abide by the Qt Code of Conduct

                          T 1 Reply Last reply 28 Jan 2017, 17:00
                          0
                          • K kshegunov
                            28 Jan 2017, 16:52

                            @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 a double. You need to convert the string to an actual number if you want to use it as such, e.g. see here.

                            T Offline
                            T Offline
                            tomy
                            wrote on 28 Jan 2017, 17:00 last edited by tomy
                            #20

                            @kshegunov

                            expression() returns an int or double value.

                            I'm pretty sure that functions returns a string, not an int, nor a double.

                            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 a QString.

                            K 1 Reply Last reply 28 Jan 2017, 21:39
                            0
                            • T tomy
                              28 Jan 2017, 16:35

                              @kshegunov

                              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?

                              V Offline
                              V Offline
                              VRonin
                              wrote on 28 Jan 2017, 17:14 last edited by
                              #21

                              @tomy Did you even try my solution?

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              T 1 Reply Last reply 28 Jan 2017, 17:43
                              0
                              • V VRonin
                                28 Jan 2017, 17:14

                                @tomy Did you even try my solution?

                                T Offline
                                T Offline
                                tomy
                                wrote on 28 Jan 2017, 17:43 last edited by
                                #22

                                @VRonin

                                Should I use it this way:

                                QString ss;
                                ss = result_box -> locale().toString(1257.147862,'f');
                                QTextStream (&ss) << expression();
                                result_box -> setText(ss);
                                

                                ?

                                V 1 Reply Last reply 28 Jan 2017, 18:36
                                0
                                • T tomy
                                  28 Jan 2017, 17:43

                                  @VRonin

                                  Should I use it this way:

                                  QString ss;
                                  ss = result_box -> locale().toString(1257.147862,'f');
                                  QTextStream (&ss) << expression();
                                  result_box -> setText(ss);
                                  

                                  ?

                                  V Offline
                                  V Offline
                                  VRonin
                                  wrote on 28 Jan 2017, 18:36 last edited by
                                  #23

                                  @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'));

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  T 1 Reply Last reply 28 Jan 2017, 18:43
                                  0
                                  • V VRonin
                                    28 Jan 2017, 18:36

                                    @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'));

                                    T Offline
                                    T Offline
                                    tomy
                                    wrote on 28 Jan 2017, 18:43 last edited by
                                    #24

                                    @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 :( :(

                                    1 Reply Last reply
                                    0
                                    • V Offline
                                      V Offline
                                      VRonin
                                      wrote on 28 Jan 2017, 18:46 last edited by
                                      #25

                                      what is result_box and what is inside expression()?

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      T 1 Reply Last reply 28 Jan 2017, 18:58
                                      0
                                      • V VRonin
                                        28 Jan 2017, 18:46

                                        what is result_box and what is inside expression()?

                                        T Offline
                                        T Offline
                                        tomy
                                        wrote on 28 Jan 2017, 18:58 last edited by tomy
                                        #26

                                        @VRonin

                                        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
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • T Offline
                                          T Offline
                                          tomy
                                          wrote on 28 Jan 2017, 19:22 last edited by tomy
                                          #27

                                          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.

                                          T 1 Reply Last reply 28 Jan 2017, 20:42
                                          0

                                          17/41

                                          28 Jan 2017, 16:40

                                          • Login

                                          • Login or register to search.
                                          17 out of 41
                                          • First post
                                            17/41
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved