[SOLVED] How to convert a double to a QString for writing into a SQL table (QString::number not working properly)?



  • Hi,

    I would like to write double values into a MySQL table.
    In general writing works and I also have declared the fields in the MySQL table as double values.

    I tried to bind the doubles directly and this works half.
    If I use something like this:

    double angle= 29.6481;
    double duration=222.333;
    
    QSqlQuery insert;
            insert.prepare("INSERT INTO messung (winkel, millisekunden)" "VALUES (:winkel, :millisekunden)");
            insert.bindValue(":winkel", angle);
            insert.bindValue(":millisekunden", QString::number(duration,'f', 2));
            insert.exec();
    

    Then I have the values 29 and 222 in my MySQL table.

    If I try this

     qDebug() << QString::number(angle,'f', 2);
    

    I get 29.00 in the console.

    How can I send the real values without rounding to my MySQL table?

    Thank you very much :-)



  • This line

    qDebug() << QString::number(angle,'f', 2);
    

    should not show '29.00' on the output unless the value has been altered since assigned the value 29.6481 when declared. It could have been truncated if assigned or cast to an integer somewhere prior to this qDebug() line perhaps.

    One option to eliminate rounding, if possible, is to have the table setup with floating point types as opposed to using a string. If the table already exists and cannot be changed then this may not be an option unless the related software is adjusted.

    On a side note another variation of number conversion to a string is this:

    qDebug() << QString("%1").arg(angle,'f', 2);
    

    It is handy if you want to add additional text with the number (this includes a new line character or possibly a tab or comma).



  • Thank you very much :-)

    You gave me the right hint.

    There was a cast before which I did not see.
    And since everything was casted to int MySQL deleted the Zeros after the '.'

    Now everything works fine and thanks for the other variation :-)


Log in to reply
 

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