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 ?

How to convert string to double ?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 4.7k Views
  • 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.
  • M Offline
    M Offline
    Mucahit
    wrote on 3 Feb 2021, 12:37 last edited by Mucahit 2 Mar 2021, 12:45
    #1

    Hi all,

    I am new in qt. I want convert string to double . My string is= "12345.54". When i used string.toDouble() i am getting this result = 12345.5 , i want this result=12345.54, why i can't this result ? Also when i changed string this = "12345.56" or "12345.57", i am getting this result=12345.6

    Regards

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Feb 2021, 12:44 last edited by
      #2

      Hi,

      How do you determine that you have that ?

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mucahit
        wrote on 3 Feb 2021, 12:47 last edited by
        #3

        My code is below:

        QString number="12345.54";
        double x=number.toDouble();
        qDebug()<<x;

        My output is= 12345.5

        J 1 Reply Last reply 3 Feb 2021, 12:52
        0
        • M Mucahit
          3 Feb 2021, 12:47

          My code is below:

          QString number="12345.54";
          double x=number.toDouble();
          qDebug()<<x;

          My output is= 12345.5

          J Offline
          J Offline
          JonB
          wrote on 3 Feb 2021, 12:52 last edited by JonB 2 Mar 2021, 14:56
          #4

          @Mucahit
          That is just an artefact of how qDebug() displays a double by default (6 digits). use QString QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' ')) const to specify more precision.

          1 Reply Last reply
          2
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 3 Feb 2021, 12:53 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

            J 1 Reply Last reply 3 Feb 2021, 12:56
            5
            • S SGaist
              3 Feb 2021, 12:53

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

              J Offline
              J Offline
              JonB
              wrote on 3 Feb 2021, 12:56 last edited by JonB 2 Mar 2021, 12:57
              #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()??

              S 1 Reply Last reply 3 Feb 2021, 13:08
              0
              • J JonB
                3 Feb 2021, 12:56

                @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()??

                S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 3 Feb 2021, 13:08 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

                J 1 Reply Last reply 3 Feb 2021, 13:13
                3
                • S SGaist
                  3 Feb 2021, 13:08

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 3 Feb 2021, 13:13 last edited by JonB 2 Mar 2021, 13:13
                  #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...? :)

                  O 1 Reply Last reply 3 Feb 2021, 13:22
                  0
                  • J JonB
                    3 Feb 2021, 13:13

                    @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...? :)

                    O Offline
                    O Offline
                    ODБOï
                    wrote on 3 Feb 2021, 13:22 last edited by ODБOï 2 Mar 2021, 13:26
                    #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 3 Feb 2021, 13:54 last edited by Mucahit 2 Mar 2021, 13:57
                      #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 ?

                      J 1 Reply Last reply 3 Feb 2021, 13:57
                      0
                      • M Mucahit
                        3 Feb 2021, 13:54

                        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 ?

                        J Offline
                        J Offline
                        JonB
                        wrote on 3 Feb 2021, 13:57 last edited by JonB 2 Mar 2021, 14:19
                        #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 3 Feb 2021, 14:39 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

                          J J 2 Replies Last reply 3 Feb 2021, 14:42
                          0
                          • M Mucahit
                            3 Feb 2021, 14:39

                            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

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 3 Feb 2021, 14:42 last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • M Mucahit
                              3 Feb 2021, 14:39

                              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

                              J Offline
                              J Offline
                              JonB
                              wrote on 3 Feb 2021, 14:49 last edited by JonB 2 Mar 2021, 14:57
                              #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 4 Feb 2021, 06:02 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 J 2 Replies Last reply 4 Feb 2021, 06:16
                                0
                                • M Mucahit
                                  4 Feb 2021, 06:02

                                  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 Offline
                                  J Offline
                                  J.Hilk
                                  Moderators
                                  wrote on 4 Feb 2021, 06:16 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 4 Feb 2021, 06:25 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
                                      4 Feb 2021, 06:02

                                      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 Offline
                                      J Offline
                                      JonB
                                      wrote on 4 Feb 2021, 09:03 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 4 Feb 2021, 12:08 last edited by
                                        #19

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

                                        1 Reply Last reply
                                        0

                                        8/19

                                        3 Feb 2021, 13:13

                                        • Login

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