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 4.0k 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.
  • 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 Offline
    JonBJ Offline
    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