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. Cannot extract a usable double or float from std::string or qstring

Cannot extract a usable double or float from std::string or qstring

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 652 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.
  • E Offline
    E Offline
    emp1953
    wrote on last edited by
    #1

    I am reading data from a csv file into a user defined structure.

     struct reqs {
            std::string n_name;
            std::string units;
            std::string amt_grams;
            std::string amt_pct;
        };
    

    I the do a pushback of this structure into a std::vector of type <reqs>

    std::vector<reqs> rvector;
    

    I print this vector out using qDebug in a loop from from 0 to < rvector.size().

    The numbers that are 1,000,000 or larger print in scientific notation 1e6.

    When I try to do math operations on these numbers they are ALWAYS off by a factor of 1000.
    I think that the exponent cannot be interpreted correctly.
    I've made separate vectors for each item in the structure of type <double> and parsed through
    the rvector loading each vector using this conversion to float

    double tmpdbl = (double)atof(rvector[i].units.c_str());
    req_units_vector.push_back(tmpdbl);
    

    Still when I try to display using qDebug or std::cout I get exponents and get the same bogus division result which is off
    by 1000.

    Any suggestions on getting ONLY doubles and no exponent representation.

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

      Please provide an example.

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

      E 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Since you have strings why not use stof ?

        It has the advantage of screaming at you if there's an issue during the conversion.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • E emp1953

          I am reading data from a csv file into a user defined structure.

           struct reqs {
                  std::string n_name;
                  std::string units;
                  std::string amt_grams;
                  std::string amt_pct;
              };
          

          I the do a pushback of this structure into a std::vector of type <reqs>

          std::vector<reqs> rvector;
          

          I print this vector out using qDebug in a loop from from 0 to < rvector.size().

          The numbers that are 1,000,000 or larger print in scientific notation 1e6.

          When I try to do math operations on these numbers they are ALWAYS off by a factor of 1000.
          I think that the exponent cannot be interpreted correctly.
          I've made separate vectors for each item in the structure of type <double> and parsed through
          the rvector loading each vector using this conversion to float

          double tmpdbl = (double)atof(rvector[i].units.c_str());
          req_units_vector.push_back(tmpdbl);
          

          Still when I try to display using qDebug or std::cout I get exponents and get the same bogus division result which is off
          by 1000.

          Any suggestions on getting ONLY doubles and no exponent representation.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @emp1953
          @SGaist suggests using stof. Since you say you are storing the numbers as doubles not floats, why not use the underlying strtod or the corresponding stod, rather than gratuitously discarding the extra precision?

          Note that the "exponent representation" is produced on output. qDebug() uses the default floating point output, which goes to exponent for small/large values, as well as limiting to 6 significant digits. You can change that to e.g. never use exponent via, say, qDebug() << QString::number(doubleNum, 'f', 12), https://doc.qt.io/qt-5/qstring.html#number-6.

          E 1 Reply Last reply
          1
          • JonBJ JonB

            @emp1953
            @SGaist suggests using stof. Since you say you are storing the numbers as doubles not floats, why not use the underlying strtod or the corresponding stod, rather than gratuitously discarding the extra precision?

            Note that the "exponent representation" is produced on output. qDebug() uses the default floating point output, which goes to exponent for small/large values, as well as limiting to 6 significant digits. You can change that to e.g. never use exponent via, say, qDebug() << QString::number(doubleNum, 'f', 12), https://doc.qt.io/qt-5/qstring.html#number-6.

            E Offline
            E Offline
            emp1953
            wrote on last edited by
            #5

            @JonB Agreed about the exponent representation on qDebug output. But when I put the numbers into a qtablewidget item they also are exponential. Not as easy for the laymen user to visualize.

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Please provide an example.

              E Offline
              E Offline
              emp1953
              wrote on last edited by
              #6

              @Christian-Ehrlicher Do you need example output or code?

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

                We want to see what you're doing so write three lines where you parse the string and output it where you think the output is wrong.

                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
                • E emp1953

                  @JonB Agreed about the exponent representation on qDebug output. But when I put the numbers into a qtablewidget item they also are exponential. Not as easy for the laymen user to visualize.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @emp1953 said in Cannot extract a usable double or float from std::string or qstring:

                  But when I put the numbers into a qtablewidget item they also are exponential.

                  At some point you want to call QString::number(doubleNum, 'f', 12). That could be when storing them as strings, or in a QStyledItemDelegate for output. This is just for the issue of displaying them.

                  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