I have a large value in MySQL. It is stored in MySQL properly as a DECIMAL(22,2). I have confirmed that the value is stored properly. If I run a query via command console I get the proper value back in the desired non-scientific form (the value and the form is: 99996543210987654321.99).
The problem is that when I run the query with Qt/C++ I don’t see how I can show the number in non-scientic notation. I get (9.99965432109877e+19) instead of (99996543210987654321.99). I want the later.
In C++/Qt This is how I extract the value from the Qt query.
(1) First, I open a database connection to MySQL with Qt.
(2) Then, I make a query to find a specific record. So far so good.
(3) Then, I extract the specific field value from the returned record found by the query using the exact code posted bellow.
*NOTE. QSqlQuery::value returns a Qvariant.
http://doc.qt.io/qt-5/qvariant.html#QVariant-30
if( query3.next() )
{
QVariant number = query3.value(query3.record().indexOf("account_balance"));
qout << number.toString() << endl;
}
I have also tried (with same result).
if( query3.next() )
{
qout<<query3.value(query3.record().indexOf("account_balance")).toString()<<endl;
}
Just to add, “qout” is just a QTextStream. I think the problem is not qout because the code bellow outputs 99996543210987654321.99 as is without scientific notation.
qout << "99996543210987654321.99" << endl;
How can I get back value 99996543210987654321.99 as is without rounding by c++/qt and without scientific notation?
.
.
.
.
.
.
EDIT:
tried at Pete’s suggestion
char string_account_balance[32] = "";
sprintf( string_account_balance, "%.2f", query3.value(query3.record().indexOf("account_balance")).toString() );
Got errors and crash. Errors:
(1) connot pass objects of non-POD type ‘class QByteArray’ through ‘…’; call will abort runtime
(2) format ‘%.2f’ expects type ‘double’, but argument 3 has type ‘int’.
QSqlQuery::value() returns a QVariant, and there is no QVariant::toLongDouble or anything like that (aside from toString(), which doesn’t work). So you have to do the conversion in MySQL–ALTER TABLE with the CHANGE option to change DECIMAL to VARCHAR or something like that, query the result, and then change it back. Otherwise, looks like you’re out of luck, esp. given the link MSalters posted.