QString to double-value



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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.