PDA

View Full Version : Retrieve full decimal value from MySql database?



adutzu89
8th March 2014, 10:29
When I retrieve decimal data from MySql if the value is Example: 10.00 it retrieves 10.
I retrieve the data using:

while(query.next()){
QString number=query.value("decimalColumn").toSomething(); //tried toString(),toDouble(),toFloat() and all are retrieving the same
}
How can I setup so I retrieve full decimal value even if its *.00 ?

I can change fairly easy if the number appears like 99, but I get numbers like 99.90, so it if I append .00 there will be the issue of numbers appearing like 99.90.00.

stampede
8th March 2014, 12:23
Convert the query value to float and use QString::number (double n, char format, int precision) (http://qt-project.org/doc/qt-4.8/qstring.html#number-2) method to convert it to string (see argument formats (http://qt-project.org/doc/qt-4.8/qstring.html#argument-formats)).
In your case QString::number(value,'f',2) should be ok


while(query.next()){
const float v = query.value("decimalColumn").toFloat();
const QString number = QString::number(v,'f',2);
...
}

adutzu89
8th March 2014, 15:12
Thank you for your help.