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. QString to double-value
Forum Updated to NodeBB v4.3 + New Features

QString to double-value

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 42.9k Views 1 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.
  • S Offline
    S Offline
    strangelookingnerd
    wrote on last edited by
    #1

    Hello there!

    For a measurement software I need to present several values to the user via Qwt.
    From predefined methods I get QStrings like this:
    @
    QString temperature = "-30.8 °C"
    QString conductivity = "4.30e^-5 mS/cm"
    @
    For using those values in a Qwt plot i need to convert them into doubles. So I need to cut off the unit and use QString::toDouble().
    I tried it by using
    @
    QString::remove(QRegExp("\D")).toDouble();
    @
    which won't work since it eg. removes the "e"-part in conductivity and so on.
    Since my solution is some kind of library, I can't use sth like QString::remove("mS/cm"). Further I can't change the output of the methods that return me those QStrings.
    Anyone having an idea how to solve this?

    1 Reply Last reply
    1
    • H Offline
      H Offline
      Hostel
      wrote on last edited by
      #2

      My first and most simple solution is: find first space and remove from space to end of string. I don't know else examples of probable data, so I don't know if this would be a good solution.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        Or, if you want to stick to your approach, use a better expression.
        @
        QString::remove(QRegExp(" .*")).toDouble();
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          And please, check the result of toDouble by using the bool* ok flag, and do something sensible if it fails. You cannot assume a QString to bool conversion always succeeds.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vezprog
            wrote on last edited by
            #5

            you could always use QString(string).split(" ") if you know that there is always going to be a space inbetween the unit specification and the number. So when you set that equal to a stringlist, your first array element will be what your looking for. Then manipulate from there.

            @
            QString temperature = "-30.8 °C"
            QString conductivity = "4.30e^-5 mS/cm"

            QStringList tempSL = temperature.split(" ");
            QStringList tempCN = conductivity.split(" ");

            double value1 = QString(tempSL[0]).toDouble();
            double value2 = QString(tempCN[0]).toDouble();
            @

            Im not sure of your application but this is how I would do it if I were possibly going to need the units in the future (which are located in the 2nd element of the stringlists). Heres the docs incase you find a better way to use it: http://developer.qt.nokia.com/doc/qt-4.8/qstring.html#split

            :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              strangelookingnerd
              wrote on last edited by
              #6

              [quote author="Lukas Geyer" date="1326285390"]Or, if you want to stick to your approach, use a better expression.
              @
              QString::remove(QRegExp(" .*")).toDouble();
              @
              [/quote]

              This is pretty much what I needed, thank you very much. Wonder why I didn't came up with it by myself :D

              [quote author="Andre" date="1326287063"]And please, check the result of toDouble by using the bool* ok flag, and do something sensible if it fails. You cannot assume a QString to bool conversion always succeeds. [/quote]

              I do, but so far never came to a point where the conversion (in my application) actually failed. Lucky me!

              [quote author="dvez43" date="1326288531"]you could always use QString(string).split(" ") if you know that there is always going to be a space inbetween the unit specification and the number. So when you set that equal to a stringlist, your first array element will be what your looking for. Then manipulate from there.

              @
              QString temperature = "-30.8 °C"
              QString conductivity = "4.30e^-5 mS/cm"

              QStringList tempSL = temperature.split(" ");
              QStringList tempCN = conductivity.split(" ");

              double value1 = QString(tempSL[0]).toDouble();
              double value2 = QString(tempCN[0]).toDouble();
              @

              Im not sure of your application but this is how I would do it if I were possibly going to need the units in the future (which are located in the 2nd element of the stringlists). Heres the docs incase you find a better way to use it: http://developer.qt.nokia.com/doc/qt-4.8/qstring.html#split

              :)
              [/quote]

              In fact this would work to. Can't tell which works better for my case. Since Qwt-plot won't need the units so far I stick to sth like this:

              @
              bool ok;
              double valueForQwt = QStringWithUnit.remove(QRegExp(" .*")).toDouble(&ok);
              if(!ok)
              //Error
              @

              Thanks to all of you!

              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