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. How to convert string to double ?
Forum Updated to NodeBB v4.3 + New Features

How to convert string to double ?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 4.8k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    You can use qSetRealNumberPrecision to ensure you have the precision you want to show the numbers you are using.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    JonBJ 1 Reply Last reply
    5
    • SGaistS SGaist

      You can use qSetRealNumberPrecision to ensure you have the precision you want to show the numbers you are using.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #6

      @SGaist
      I didn't know about your way. How do you access qDebug()'s QTextStream to call yours? Are you saying you just call qDebug().qSetRealNumberPrecision()??

      SGaistS 1 Reply Last reply
      0
      • JonBJ JonB

        @SGaist
        I didn't know about your way. How do you access qDebug()'s QTextStream to call yours? Are you saying you just call qDebug().qSetRealNumberPrecision()??

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @JonB

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        3
        • SGaistS SGaist

          @JonB

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #8

          @SGaist
          Hmm. Thank you. I find this use of C++ streams (and <<) hard to get my head around, logically! If you can go qDebug() << value to print out a value, I find it conceptually difficult to send qSetRealNumberPrecision(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 the qDebug() stream from then onward? Or...? :)

          ODБOïO 1 Reply Last reply
          0
          • JonBJ JonB

            @SGaist
            Hmm. Thank you. I find this use of C++ streams (and <<) hard to get my head around, logically! If you can go qDebug() << value to print out a value, I find it conceptually difficult to send qSetRealNumberPrecision(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 the qDebug() stream from then onward? Or...? :)

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #9

            @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
            
            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mucahit
              wrote on last edited by Mucahit
              #10

              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 correct

              ui->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 ?

              JonBJ 1 Reply Last reply
              0
              • M Mucahit

                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 correct

                ui->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 ?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #11

                @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 with qDebug(), QLabel::setText() or anywhere else a string is wanted.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Mucahit
                  wrote on last edited by
                  #12

                  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 correct

                  but 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

                  jsulmJ JonBJ 2 Replies Last reply
                  0
                  • M Mucahit

                    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 correct

                    but 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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • M Mucahit

                      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 correct

                      but 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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #14

                      @Mucahit
                      You are simply mixing up qSetRealNumberPrecision() with QString.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 affects qDebug(), 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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mucahit
                        wrote on last edited by
                        #15

                        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

                        J.HilkJ JonBJ 2 Replies Last reply
                        0
                        • M Mucahit

                          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

                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #16

                          @Mucahit

                          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();
                          }
                          

                          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.

                          1 Reply Last reply
                          4
                          • M Offline
                            M Offline
                            Mucahit
                            wrote on last edited by
                            #17

                            Ohh, it really works. I apologize for bothering you. I am changing the topic to solved. Thank you so much !!!

                            1 Reply Last reply
                            0
                            • M Mucahit

                              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

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #18

                              @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, like QString::arg() or @J-Hilk's QLocale::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.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Mucahit
                                wrote on last edited by
                                #19

                                I understand everything much better now. Thank you again for everything sir.

                                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