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 30.1k 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.
  • tomyT tomy

    @VRonin

    Should I use it this way:

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

    ?

    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on 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

    tomyT 1 Reply Last reply
    0
    • VRoninV VRonin

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

      tomyT Offline
      tomyT Offline
      tomy
      wrote on 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
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on 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

        tomyT 1 Reply Last reply
        0
        • VRoninV VRonin

          what is result_box and what is inside expression()?

          tomyT Offline
          tomyT Offline
          tomy
          wrote on 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
          • tomyT Offline
            tomyT Offline
            tomy
            wrote on 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.

            tomyT 1 Reply Last reply
            0
            • tomyT tomy

              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.

              tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by tomy
              #28

              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));
              
              K 1 Reply Last reply
              0
              • tomyT tomy

                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));
                
                K Offline
                K Offline
                koahnig
                wrote on last edited by koahnig
                #29

                @tomy
                Maybe you should have a look to the docs as well. E.g. here

                In case of integer assignment that would be:

                int i = 10;
                result_box -> setText ( QString::number( i ) );
                

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • tomyT tomy

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #30

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

                  Read and abide by the Qt Code of Conduct

                  tomyT 2 Replies Last reply
                  0
                  • kshegunovK kshegunov

                    @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 with qFuzzyCompare, 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');
                    
                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by
                    #31
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @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 with qFuzzyCompare, 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');
                      
                      tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by tomy
                      #32

                      @kshegunov

                      What you want is to have the if with qFuzzyCompare, 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');
                      

                      Sorry, I don't know how I tested it but it doesn't work as it's expected! :(
                      for example:
                      2 + 3 = 5 OK
                      10 ^ 6 = 1000000 OK
                      2 + 1.5 = 3.500000 !!!

                      That is, the function QString::number(expression(), 'f'); (with the default precision 6) shows all range of precision whether it's need or not!

                      Maybe it's what we need:
                      http://doc.qt.io/qt-5/qtextstream.html#setRealNumberNotation
                      But how to use it? Docs doesn't offer a little example of it to show how beginners should use it. !!!!!!
                      (Docs are not for learners/beginners, they are for professionals — if they are professional, so they don't need Docs much => Docs are not useful)

                      kshegunovK 1 Reply Last reply
                      0
                      • tomyT tomy

                        @kshegunov

                        What you want is to have the if with qFuzzyCompare, 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');
                        

                        Sorry, I don't know how I tested it but it doesn't work as it's expected! :(
                        for example:
                        2 + 3 = 5 OK
                        10 ^ 6 = 1000000 OK
                        2 + 1.5 = 3.500000 !!!

                        That is, the function QString::number(expression(), 'f'); (with the default precision 6) shows all range of precision whether it's need or not!

                        Maybe it's what we need:
                        http://doc.qt.io/qt-5/qtextstream.html#setRealNumberNotation
                        But how to use it? Docs doesn't offer a little example of it to show how beginners should use it. !!!!!!
                        (Docs are not for learners/beginners, they are for professionals — if they are professional, so they don't need Docs much => Docs are not useful)

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #33

                        What you want to do in the floating point case is not at all trivial. Here you can read on that topic if you are willing to digest the math.

                        The simpler but inefficient approach is to save the floating point text representation and just discard all the trailing 0 characters manually.

                        EDIT:
                        Here's (probably) the most concise way, but requires knowledge of regular expressions, which is yet again for you to read on to understand how or why it works:

                        if (qFuzzyCompare(expression(), static_cast<qint64>(expression()))
                            ss = QString::number(static_cast<qint64>(expression()));
                        else  {
                            ss = QString::number(expression(), 'f', 17);
                            ss.replace(QRegularExpression("\\.?0+$"), "");
                        }
                        

                        (Docs are not for learners/beginners, they are for professionals — if they are professional, so they don't need Docs much => Docs are not useful)

                        You got that backwards. The point of the Qt docs is they are a documentation for Qt itself, they will give no instruction into C++, its types, the types' memory layout and other such technical topics. If you are in need of that, then you need to look elsewhere, for example http://en.cppreference.com/w/

                        Read and abide by the Qt Code of Conduct

                        tomyT 1 Reply Last reply
                        1
                        • kshegunovK kshegunov

                          What you want to do in the floating point case is not at all trivial. Here you can read on that topic if you are willing to digest the math.

                          The simpler but inefficient approach is to save the floating point text representation and just discard all the trailing 0 characters manually.

                          EDIT:
                          Here's (probably) the most concise way, but requires knowledge of regular expressions, which is yet again for you to read on to understand how or why it works:

                          if (qFuzzyCompare(expression(), static_cast<qint64>(expression()))
                              ss = QString::number(static_cast<qint64>(expression()));
                          else  {
                              ss = QString::number(expression(), 'f', 17);
                              ss.replace(QRegularExpression("\\.?0+$"), "");
                          }
                          

                          (Docs are not for learners/beginners, they are for professionals — if they are professional, so they don't need Docs much => Docs are not useful)

                          You got that backwards. The point of the Qt docs is they are a documentation for Qt itself, they will give no instruction into C++, its types, the types' memory layout and other such technical topics. If you are in need of that, then you need to look elsewhere, for example http://en.cppreference.com/w/

                          tomyT Offline
                          tomyT Offline
                          tomy
                          wrote on last edited by
                          #34

                          @kshegunov

                          The point of the Qt docs is they are a documentation for Qt itself, they will give no instruction into C++, its types, the types' memory layout and other such technical topics.

                          Thanks but I didn't talk about C++, but Docs.
                          I thought we can look at the Docs as a set of instructions useful for learners to be used to Qt, because they have been frequently suggested to new comers of Qt for reading.
                          Thanks also for your code.

                          jsulmJ kshegunovK 2 Replies Last reply
                          -1
                          • tomyT tomy

                            @kshegunov

                            The point of the Qt docs is they are a documentation for Qt itself, they will give no instruction into C++, its types, the types' memory layout and other such technical topics.

                            Thanks but I didn't talk about C++, but Docs.
                            I thought we can look at the Docs as a set of instructions useful for learners to be used to Qt, because they have been frequently suggested to new comers of Qt for reading.
                            Thanks also for your code.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #35

                            @tomy Qt documentation is not a set of instructions and it will never be. It is just not possible to have instructions for every use case you can imagine.
                            Qt documentation documents Qt API and provides some examples. For specific use cases you need to think about it, read Qt documentation, try out. This is same for all frameworks I was using so far.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • K Offline
                              K Offline
                              kenchan
                              wrote on last edited by kenchan
                              #36

                              You are wasting your time trying to learn programming and c++ just by looking at the Qt docs... talk about an uphill hike :p

                              1 Reply Last reply
                              1
                              • tomyT tomy

                                @kshegunov

                                The point of the Qt docs is they are a documentation for Qt itself, they will give no instruction into C++, its types, the types' memory layout and other such technical topics.

                                Thanks but I didn't talk about C++, but Docs.
                                I thought we can look at the Docs as a set of instructions useful for learners to be used to Qt, because they have been frequently suggested to new comers of Qt for reading.
                                Thanks also for your code.

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by kshegunov
                                #37

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

                                I thought we can look at the Docs as a set of instructions useful for learners to be used to Qt, because they have been frequently suggested to new comers of Qt for reading.

                                You can, but the implication is you have a decent knowledge of C++ before that, as Qt is a library that's written and intended to be used from (among others) C++ code. So these are two separate issues you need to address. Learning about Qt without a good fundament is (and always will be) a very, very hard thing to do. Think about it like this, you wouldn't start writing a book in a foreign language, unless you're very intimately familiar with the actual language, right?

                                Read and abide by the Qt Code of Conduct

                                tomyT 1 Reply Last reply
                                3
                                • kshegunovK kshegunov

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

                                  I thought we can look at the Docs as a set of instructions useful for learners to be used to Qt, because they have been frequently suggested to new comers of Qt for reading.

                                  You can, but the implication is you have a decent knowledge of C++ before that, as Qt is a library that's written and intended to be used from (among others) C++ code. So these are two separate issues you need to address. Learning about Qt without a good fundament is (and always will be) a very, very hard thing to do. Think about it like this, you wouldn't start writing a book in a foreign language, unless you're very intimately familiar with the actual language, right?

                                  tomyT Offline
                                  tomyT Offline
                                  tomy
                                  wrote on last edited by
                                  #38

                                  @kshegunov
                                  First off, I like your attitude. And I wish we all believe in Democracy. Indeed, I use a book for learning Qt. But it's sometimes possible to refer to a good resource for a topic. I know Docs are in a high position in Qt folk's perspectives, but if I can use democracy and say my opinion, I say, "I have not found them useful up to now".
                                  It may change. I've joined Qt just recently.
                                  Thanks for your talks.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • tomyT tomy

                                    @kshegunov
                                    First off, I like your attitude. And I wish we all believe in Democracy. Indeed, I use a book for learning Qt. But it's sometimes possible to refer to a good resource for a topic. I know Docs are in a high position in Qt folk's perspectives, but if I can use democracy and say my opinion, I say, "I have not found them useful up to now".
                                    It may change. I've joined Qt just recently.
                                    Thanks for your talks.

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #39

                                    @tomy It is perfectly fine to tell others your opinion. But it is as well perfectly fine for others to disagree with you :-)

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    1
                                    • J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #40

                                      Hi,

                                      for your specific situation, I would suggest something like:

                                      QString truncValue(double value, int prec)
                                      {
                                          QString sReturn = QString::number(value,'f',prec);
                                          if(sReturn.endsWith("0")){
                                              while(sReturn.endsWith("0"))
                                                  sReturn.remove(sReturn.length()-1,1);
                                      
                                              if(sReturn.endsWith("."))
                                                  sReturn.remove(sReturn.length()-1,1);
                                          }
                                          
                                          return sReturn;
                                      }
                                      

                                      But, this is successively chaining a lot of string operations. The previously mentions methods are probably better.


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      tomyT 1 Reply Last reply
                                      1
                                      • J.HilkJ J.Hilk

                                        Hi,

                                        for your specific situation, I would suggest something like:

                                        QString truncValue(double value, int prec)
                                        {
                                            QString sReturn = QString::number(value,'f',prec);
                                            if(sReturn.endsWith("0")){
                                                while(sReturn.endsWith("0"))
                                                    sReturn.remove(sReturn.length()-1,1);
                                        
                                                if(sReturn.endsWith("."))
                                                    sReturn.remove(sReturn.length()-1,1);
                                            }
                                            
                                            return sReturn;
                                        }
                                        

                                        But, this is successively chaining a lot of string operations. The previously mentions methods are probably better.

                                        tomyT Offline
                                        tomyT Offline
                                        tomy
                                        wrote on last edited by
                                        #41

                                        @J.Hilk
                                        Thank you for the code.
                                        honestly, I solved the issue two or three days ago. When I found I can't rely on a function offered by Qt on this specific problem, I returned to my old friend, C++, and solved the issue using it.
                                        But I appreciate your paying attention to the problem.

                                        1 Reply Last reply
                                        0

                                        • Login

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