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. Operations with QString and int

Operations with QString and int

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 7 Posters 1.9k 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.
  • J.HilkJ J.Hilk

    @Swati777999

    what's wrong with

    QString myStr = "31415";
    int myInt = myStr.toInt();
    myStr = QString::number(++myInt);
    

    ?

    Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #11

    @J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    KroMignonK J.HilkJ 2 Replies Last reply
    0
    • Swati777999S Swati777999

      @J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #12

      @Swati777999 said in Operations with QString and int:

      Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

      Please again, don't program in C++ as you would do it in python, this will not work!
      There is no implicit convertion from string <=> real or string <=> integer in C++.
      I you want to program in python style, use python not C++ or you will only go frustrated.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      Swati777999S 1 Reply Last reply
      0
      • Swati777999S Swati777999

        @J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

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

        @Swati777999

        template<typename T>
        QString addOne(const QString &string)
        {
            auto value = std::is_floating_point<T>::value ? string.toDouble() : string.toInt();
            return QString::number(++value);
        }
        
        int main(int argc, char *argv[])
        {
            qDebug() << addOne<int>("12345") << addOne<float>("1234.56");
        }
        

        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
          mchinand
          wrote on last edited by
          #14

          It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.

          Swati777999S 1 Reply Last reply
          1
          • M mchinand

            It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #15

            @mchinand said in Operations with QString and int:

            It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.

            Yes, actually my true intention for putting this Question was about dealing with rounding of real values but I also wanted to be clear about the string and int conversion.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            1 Reply Last reply
            0
            • KroMignonK KroMignon

              @Swati777999 said in Operations with QString and int:

              Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

              Please again, don't program in C++ as you would do it in python, this will not work!
              There is no implicit convertion from string <=> real or string <=> integer in C++.
              I you want to program in python style, use python not C++ or you will only go frustrated.

              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #16

              @KroMignon
              My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              KroMignonK 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @Swati777999 said in Operations with QString and int:

                With this modified code , I got the following result-

                It is doing what you have coded.

                QString myStr = "31415";
                 int myStr_sz = myStr.size(); // => myStr = 5
                 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                 last_dig+=1; /// => last_dig = 6
                 myStr[myStr_sz-1] = last_dig;  // change  myStr[myStr_sz-1] from '5' to '\x06'
                 qDebug()<<"New String is =" <<myStr;
                

                EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'

                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on last edited by
                #17

                @KroMignon said in Operations with QString and int:

                @Swati777999 said in Operations with QString and int:

                With this modified code , I got the following result-

                It is doing what you have coded.

                QString myStr = "31415";
                 int myStr_sz = myStr.size(); // => myStr = 5
                 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                 last_dig+=1; /// => last_dig = 6
                 myStr[myStr_sz-1] = last_dig;  // change  myStr[myStr_sz-1] from '5' to '\x06'
                 qDebug()<<"New String is =" <<myStr;
                

                EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'

                EXAMPLE-1

                int x = 56;
                  QString str = QString::number(x);
                  qDebug()<<"x = " <<str; // x ="56"
                

                Using Example 1 -
                I modified the code

                 ```
                 QString myStr = "31415";
                 qDebug()<<"old String is =" <<myStr;  //31415
                 int myStr_sz = myStr.size(); // => myStr = 5
                 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                 last_dig+=1; // => last_dig = 6
                 qDebug()<<"incremented last dig is="<<last_dig;  // 6
                 QString str_last =QString::number(last_dig);  
                 qDebug()<<"incremented last dig after converting to string is="<<last_dig; // ==> 6
                 myStr[myStr_sz-1] = last_dig; 
                 qDebug()<<"New String is =" <<myStr;
                

                trial-2_02.03.2022.png

                I am getting the same old result.

                Another way I'm thinking is that, popping out the last digit from the string and appending the incremented last digit . I tried it but didn't work. Please give suggestion .

                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sivan
                  wrote on last edited by
                  #18

                  @Swati777999 said in Operations with QString and int:

                  You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                   myStr[myStr_sz-1] = last_dig + '0'; 
                  

                  But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                  Swati777999S 2 Replies Last reply
                  1
                  • Swati777999S Swati777999

                    @KroMignon
                    My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #19

                    @Swati777999 said in Operations with QString and int:

                    My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]

                    So we are totally in opposite about the way to do: programming C++ but thinking the program with Python in mind is, for me, the worst way to do.

                    C++ and python are so far apart in almost every aspect of programming, I can't see how this can work.
                    If we are only talking about validating an algorithm, why not. But transposing from Python to C++ is not a simple matter. That alone amounts to a new development.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    3
                    • S Sivan

                      @Swati777999 said in Operations with QString and int:

                      You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                       myStr[myStr_sz-1] = last_dig + '0'; 
                      

                      But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                      Swati777999S Offline
                      Swati777999S Offline
                      Swati777999
                      wrote on last edited by Swati777999
                      #20

                      @Sivan said in Operations with QString and int:

                      @Swati777999 said in Operations with QString and int:

                      You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                       myStr[myStr_sz-1] = last_dig + '0'; 
                      

                      But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                      Yes, your suggestion works perfectly well.

                          QString myStr = "31415";
                          qDebug()<<"old String is =" <<myStr;
                          int myStr_int = myStr.toInt();
                          myStr_int +=1;
                          qDebug()<<"New int value is =" <<myStr_int;
                      

                      trial-6(a)_02.03.png

                      Trying with real vlaues

                          QString myStr = "99.99";
                          qDebug()<<"old String is =" <<myStr;
                          int myStr_int = myStr.toDouble();   //  And:100  [.toFloat() ]
                          myStr_int +=1;
                          qDebug()<<"New int value is =" <<myStr_int;
                      

                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                      1 Reply Last reply
                      0
                      • S Sivan

                        @Swati777999 said in Operations with QString and int:

                        You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                         myStr[myStr_sz-1] = last_dig + '0'; 
                        

                        But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                        Swati777999S Offline
                        Swati777999S Offline
                        Swati777999
                        wrote on last edited by
                        #21

                        @Sivan said in Operations with QString and int:

                        @Swati777999 said in Operations with QString and int:

                        You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                         myStr[myStr_sz-1] = last_dig + '0'; 
                        

                        But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                        Here's my try with real values

                          QString myStr = "99.20";
                            qDebug()<<"old String is =" <<myStr;
                            int indx = myStr.indexOf(".");
                            myStr.remove('.');
                            float myStr_int = myStr.toFloat();   //100  
                            myStr_int +=1;
                            QString myStrNew = QString::number(myStr_int);
                            myStrNew.insert(indx,".");
                            qDebug()<<"New int value is =" <<myStrNew.toFloat();
                        

                        trial-6(b)_02.03.png

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #22

                          The output 99.21 is correct don't know why you do such strange stuff just to add 0.01 to a float value though)

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          Swati777999S 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            The output 99.21 is correct don't know why you do such strange stuff just to add 0.01 to a float value though)

                            Swati777999S Offline
                            Swati777999S Offline
                            Swati777999
                            wrote on last edited by
                            #23

                            @Christian-Ehrlicher for some reason, I was not getting the correct result at first, this is why I opted for the first method in the first place. Now, I realize that more than coding, I have to be cautious about what I am displaying on the screen. I also, have to avoid my tendency to arrive at any conclusion so quickly.

                            “ In order to be irreplaceable, one must always be different” – Coco Chanel

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #24

                              As I said - I don't understand what you're doing. Your stuff will e.g. not work for 99.99...

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              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