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