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

        21/23

        25 Jan 2023, 09:28

        • Login

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