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. converting from float or double to QString results in output being 0
Forum Updated to NodeBB v4.3 + New Features

converting from float or double to QString results in output being 0

Scheduled Pinned Locked Moved Solved General and Desktop
c++qstringconversionline editfloat
23 Posts 4 Posters 3.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.
  • jsulmJ jsulm

    @Dean21 Something like msg_as_Qstring.remove(QChar("\u0000"));

    D Offline
    D Offline
    Dean21
    wrote on last edited by
    #12

    @jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part

    msg_as_Qstring.remove(("[\u0000]."));
    

    but it still has it attached on the end here is the output

    Topic:  "Light_Level" , Msg:  "5.73628\u0000"
    test value is "5.73628\u0000"
    Qstring as float  "5.73628\u0000"
    before amp 0
    amp multiplier 10
    msg as qstring to double 0
    msg as qstring conv "0"
    
    JonBJ jsulmJ 2 Replies Last reply
    0
    • D Dean21

      @jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part

      msg_as_Qstring.remove(("[\u0000]."));
      

      but it still has it attached on the end here is the output

      Topic:  "Light_Level" , Msg:  "5.73628\u0000"
      test value is "5.73628\u0000"
      Qstring as float  "5.73628\u0000"
      before amp 0
      amp multiplier 10
      msg as qstring to double 0
      msg as qstring conv "0"
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #13

      @Dean21 said in converting from float or double to QString results in output being 0:

      msg_as_Qstring.remove(("[\u0000]."));

      How/what do you expect it to match with that . at the end?

      Just chop off the last character, no regular expression, no? Did you find void QString::chop(qsizetype n) ?

      If you want to find out whether this will make any difference try

      msg_as_Qstring = "5.73628";
      

      first and then you will know!

      1 Reply Last reply
      0
      • Kent-DorfmanK Kent-Dorfman

        so what is (float)5.7 / (int)10?

        Therein is the reason you are getting zero!

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

        @Kent-Dorfman said in converting from float or double to QString results in output being 0:

        so what is (float)5.7 / (int)10?

        Umm, that would be 0.57(not 0).... ! Dividing a float by an int does not convert to int! :)

        1 Reply Last reply
        2
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #15

          I stand corrected. I was certain the type of the denominator governed the result. Too much time in embedded kernel domain. D'Oh!

          1 Reply Last reply
          2
          • D Dean21

            @jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part

            msg_as_Qstring.remove(("[\u0000]."));
            

            but it still has it attached on the end here is the output

            Topic:  "Light_Level" , Msg:  "5.73628\u0000"
            test value is "5.73628\u0000"
            Qstring as float  "5.73628\u0000"
            before amp 0
            amp multiplier 10
            msg as qstring to double 0
            msg as qstring conv "0"
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #16

            @Dean21 said in converting from float or double to QString results in output being 0:

            msg_as_Qstring.remove(("[\u0000]."));

            Why did you put [,] and dot there?
            I suggested this:

            msg_as_Qstring.remove(QChar("\u0000"));
            

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

            D 1 Reply Last reply
            0
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #17

              if in fact the conversion is being broken by the unicode null terminator then consider an intermediate conversion thru (char*) instead.

              QString num(byteArray.data());

              since char arrays have no defined length the constructor shoudl use the null as a string terminator, not as part of the string (as is the case when converting from a container that has an internal size field).

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Dean21 said in converting from float or double to QString results in output being 0:

                msg_as_Qstring.remove(("[\u0000]."));

                Why did you put [,] and dot there?
                I suggested this:

                msg_as_Qstring.remove(QChar("\u0000"));
                
                D Offline
                D Offline
                Dean21
                wrote on last edited by
                #18

                @jsulm sorry the "." was a type but i did try both methods and get a lot of errors about invalid conversion

                JonBJ 1 Reply Last reply
                0
                • D Dean21

                  @jsulm sorry the "." was a type but i did try both methods and get a lot of errors about invalid conversion

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #19

                  @Dean21
                  Already said. Don't know why you don't do the following (takes 10 seconds):

                  1. Try msg_as_Qstring = "5.73628"; and see if it works now. If it does
                  2. msg_as_Qstring.chop(1);.

                  Then you will know whether that is the issue....

                  D 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Dean21
                    Already said. Don't know why you don't do the following (takes 10 seconds):

                    1. Try msg_as_Qstring = "5.73628"; and see if it works now. If it does
                    2. msg_as_Qstring.chop(1);.

                    Then you will know whether that is the issue....

                    D Offline
                    D Offline
                    Dean21
                    wrote on last edited by
                    #20

                    @JonB I just did sorry didnt reply sooner I had to wait this worked great, thank you for your help and everyone else who helped me with this

                    JonBJ 1 Reply Last reply
                    1
                    • D Dean21

                      @JonB I just did sorry didnt reply sooner I had to wait this worked great, thank you for your help and everyone else who helped me with this

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #21

                      @Dean21
                      Glad it's working for you now. That's the steps I would have taken to get it working.

                      @Kent-Dorfman said earlier

                      I question whether removing non-numeric char at end of QSTring is necessary for the numeric conversion problem since all the conversion code I'm familiar with scans digits until it finds an char that can't be part of the number, and then returns everything up to that point.

                      But, for whatever reason, conversions to numeric from QString state:

                      Warning: The QString content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error.

                      So that's how they do it --- all characters in the string must be valid. You could use:

                      bool ok;
                      double val = msg_as_Qstring.toDouble(&ok);
                      if (!ok)
                          qDebug() << "Conversion failed!";
                      
                      D 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Dean21
                        Glad it's working for you now. That's the steps I would have taken to get it working.

                        @Kent-Dorfman said earlier

                        I question whether removing non-numeric char at end of QSTring is necessary for the numeric conversion problem since all the conversion code I'm familiar with scans digits until it finds an char that can't be part of the number, and then returns everything up to that point.

                        But, for whatever reason, conversions to numeric from QString state:

                        Warning: The QString content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error.

                        So that's how they do it --- all characters in the string must be valid. You could use:

                        bool ok;
                        double val = msg_as_Qstring.toDouble(&ok);
                        if (!ok)
                            qDebug() << "Conversion failed!";
                        
                        D Offline
                        D Offline
                        Dean21
                        wrote on last edited by
                        #22

                        @JonB I see that makes sense, thanks for the additional info and help :)

                        1 Reply Last reply
                        0
                        • Kent-DorfmanK Offline
                          Kent-DorfmanK Offline
                          Kent-Dorfman
                          wrote on last edited by
                          #23

                          @JonB said in converting from float or double to QString results in output being 0:

                          Warning: The QString content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error.

                          In which case RTFM is the rule of the day! but I'm lazy.

                          1 Reply Last reply
                          1

                          • Login

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