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 deal with numbers containing a comma?
Forum Updated to NodeBB v4.3 + New Features

how to deal with numbers containing a comma?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.0k Views 1 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by Natural_Bugger
    #1

    Hi, as a follow up on another topic a new issue arose.
    https://forum.qt.io/topic/108209/qlineedit-autofill/13.

    the following operation returns sometimes fractal numbers containing a comma.

    ...... (baseNumber/1000000.0);
    

    i can't do further operations depending on the results of the code above.
    "0,00012", does not further compute
    "1e-05", does further compute.

    do you need to use signed variables?
    special measurements when handling big numbers?

    how to turn "1e-05" this into "0,0001" again?

    JonBJ 1 Reply Last reply
    0
    • N Natural_Bugger

      Hi, as a follow up on another topic a new issue arose.
      https://forum.qt.io/topic/108209/qlineedit-autofill/13.

      the following operation returns sometimes fractal numbers containing a comma.

      ...... (baseNumber/1000000.0);
      

      i can't do further operations depending on the results of the code above.
      "0,00012", does not further compute
      "1e-05", does further compute.

      do you need to use signed variables?
      special measurements when handling big numbers?

      how to turn "1e-05" this into "0,0001" again?

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

      @Natural_Bugger
      Have you looked at https://doc.qt.io/qt-5/qstring.html#argument-formats ? I suspect you want f format, the default is probably g? And if you have commas in your input/output, look at the locale you are using.

      N 1 Reply Last reply
      0
      • JonBJ JonB

        @Natural_Bugger
        Have you looked at https://doc.qt.io/qt-5/qstring.html#argument-formats ? I suspect you want f format, the default is probably g? And if you have commas in your input/output, look at the locale you are using.

        N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by
        #3

        @JonB

        thnx for your pront reply:

        the main problem is that i can do further computing on numbers containing a comma, the results of the equation above, before putting it back into a a QLineEdit form field.
        "inf" shows up in the QlineEdit form field.

        JonBJ 1 Reply Last reply
        0
        • N Natural_Bugger

          @JonB

          thnx for your pront reply:

          the main problem is that i can do further computing on numbers containing a comma, the results of the equation above, before putting it back into a a QLineEdit form field.
          "inf" shows up in the QlineEdit form field.

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

          @Natural_Bugger
          Yes, you need to convert (comma) string representation back to the actual number (float/double) before you can do computations on it.

          N 1 Reply Last reply
          0
          • JonBJ JonB

            @Natural_Bugger
            Yes, you need to convert (comma) string representation back to the actual number (float/double) before you can do computations on it.

            N Offline
            N Offline
            Natural_Bugger
            wrote on last edited by
            #5

            @JonB

            this is what is what i do.

                float solution = 0.0;
                QString one = ui->lineEdit_1->text();
                float floatOne = one.toFloat();
                QString two = ui->lineEdit_2->text();
                float floatTwo = twor.toFloat();
            
                solution = 1/(2 * M_PI * floatOne * floatTwo); // it stalls here.
                ui->lineEdit_3->->setText(QString::number(solution, 'f', 10));
            

            this occurs after operation from the other thread.

            jsulmJ JonBJ 2 Replies Last reply
            0
            • N Natural_Bugger

              @JonB

              this is what is what i do.

                  float solution = 0.0;
                  QString one = ui->lineEdit_1->text();
                  float floatOne = one.toFloat();
                  QString two = ui->lineEdit_2->text();
                  float floatTwo = twor.toFloat();
              
                  solution = 1/(2 * M_PI * floatOne * floatTwo); // it stalls here.
                  ui->lineEdit_3->->setText(QString::number(solution, 'f', 10));
              

              this occurs after operation from the other thread.

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

              @Natural_Bugger said in how to deal with numbers containing a comma?:

              this occurs after operation from the other thread

              What does this other thread do?
              Does it access UI?

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

              N 1 Reply Last reply
              2
              • N Natural_Bugger

                @JonB

                this is what is what i do.

                    float solution = 0.0;
                    QString one = ui->lineEdit_1->text();
                    float floatOne = one.toFloat();
                    QString two = ui->lineEdit_2->text();
                    float floatTwo = twor.toFloat();
                
                    solution = 1/(2 * M_PI * floatOne * floatTwo); // it stalls here.
                    ui->lineEdit_3->->setText(QString::number(solution, 'f', 10));
                

                this occurs after operation from the other thread.

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

                @Natural_Bugger
                Apart from what @jsulm asks you...

                ...QString::toFloat() won't tell you if it succeeds. You need to pass in the &ok parameter and check that, see the docs. Plus docs tell you

                The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toFloat()

                For historical reasons, this function does not handle thousands group separators. If you need to convert such numbers, use QLocale::toFloat().

                1 Reply Last reply
                3
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by SGaist
                  #8

                  Hi,

                  Why not use QSpinBox and/or QDoubleSpinBox ?

                  That way you don't need to go back and forth with strings.

                  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
                  • jsulmJ jsulm

                    @Natural_Bugger said in how to deal with numbers containing a comma?:

                    this occurs after operation from the other thread

                    What does this other thread do?
                    Does it access UI?

                    N Offline
                    N Offline
                    Natural_Bugger
                    wrote on last edited by
                    #9

                    @jsulm

                    thnx for your reply.

                    sorry, i meant the forum thread: link text

                    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