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

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.8k 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.
  • K Kent-Dorfman
    24 Jan 2023, 15:40

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

    Therein is the reason you are getting zero!

    J Offline
    J Offline
    JonB
    wrote on 24 Jan 2023, 16:28 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
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 24 Jan 2023, 18:52 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
        24 Jan 2023, 16:20

        @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"
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 25 Jan 2023, 05:48 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 25 Jan 2023, 09:03
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 25 Jan 2023, 08:55 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
          • J jsulm
            25 Jan 2023, 05:48

            @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 25 Jan 2023, 09:03 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

            J 1 Reply Last reply 25 Jan 2023, 09:13
            0
            • D Dean21
              25 Jan 2023, 09:03

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

              J Offline
              J Offline
              JonB
              wrote on 25 Jan 2023, 09:13 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 25 Jan 2023, 09:17
              1
              • J JonB
                25 Jan 2023, 09:13

                @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 25 Jan 2023, 09:17 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

                J 1 Reply Last reply 25 Jan 2023, 09:28
                1
                • D Dean21
                  25 Jan 2023, 09:17

                  @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

                  J Offline
                  J Offline
                  JonB
                  wrote on 25 Jan 2023, 09:28 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 25 Jan 2023, 09:40
                  1
                  • J JonB
                    25 Jan 2023, 09:28

                    @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 25 Jan 2023, 09:40 last edited by
                    #22

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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kent-Dorfman
                      wrote on 25 Jan 2023, 13:33 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

                      23/23

                      25 Jan 2023, 13:33

                      • Login

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