Qt Code:
double value; while (query.next()) { value = query.value(0).toDouble(); cout << value << "\n"; } // Or (although it is dumb to do this if the value is already stored as a floating point REAL) while (query.next()) { value = query.value(0).toString(); cout << value.toDouble() << "\n"; }To copy to clipboard, switch view to plain text mode
I'm not going to tell you how to add up the values to get the sum. If you don't know how to do that, you need to go back and hit the C++ books.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Ok, thanks for your support.
"the solution" after a day cracking my brain:
Note: the numbers stored in the table can not be in the format "000.000,00" or "000000,00" , but "000,000" or "000000" (the point/comma gave me problems in that case)Qt Code:
double a = 0; while (query2.next()) { a = a + query2.value(5).toDouble(); cout << a; }To copy to clipboard, switch view to plain text mode
Last edited by juliano.gomes; 16th December 2016 at 01:13.
Wrong. If the SQL data type for the column in your table is "REAL", then they are stored in binary floating point, not in any of the string formats you list here. It is the conversion your code does after retrieving the value from the database that adds the formatting.Note: the numbers stored in the table can not be in the format "000.000,00" or "000000,00" , but "000,000" or "000000"
When your code converts the return value from the query to a string (using QSqlQuery::value() and QVariant::toString()) and displays the resulting QString as you showed in your original post, Qt converts the double to a string according to its default floating point format and your locale. It is your locale that inserts the "," and "." characters into the string, and the default format that decides how many digits of precision after the decimals separator ("," in your locale, "." in mine).
Look, we aren't here to teach you how to write basic C++. I gave you code that retrieves the values from your table as floating point numbers (doubles in this case). If you seriously don't know how to write a loop that adds those numbers up, then you do need to learn the basics of C++ before you try to write Qt code.Ok, thanks for your support.
Last edited by d_stranz; 16th December 2016 at 17:24.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks