QT Currency Issue

• Hi all, I have a value stored in a sql database that represents money. When I get the value and convert it to double or float if said value ends in .00 it drops it so 125.00 becomes 125 no big issue, I can deal with this. However when I add the second value to the figure it rounds so 125.00 + 5.95 becomes 131 instead of 130.95. How can I stop this from happening?

The code I use to convert the value from sql is ..

double value = sql.value(0).toString().remove(",").toDouble(); // Have to remove the comma's or it fails

Thanks

• @Chrisw01 said in QT Currency Issue:

however when I add the second value to the figure it rounds so 125.00 + 5.95 becomes 131 instead of 130.95.

And you are using doubles for all of the variables ?
double r1=125.0;
double r2=5.95;
qDebug()<< r1+r2;

• Yes, both values are from a database.

The value in the first database is even, say 125.00 as shown by qDebug();

qDebug() << sql.value(0).toString(); // shows 125.00

qDebug() << sql.value(0).toString().toDouble(); // shows 125

And then I have

double r1 = sql.value(0).toString().remove(",").toDouble();  // just ends up being 125
double r2 = ui->lineEdit.Text().remove(",").toDouble();  // ends up being 5.95

double result = r1 + r2; // Result ends up being 131 instead of 130.95;

If I change the 5.95 to 5.45 then it ends up being 130 For some reason it's rounding and removing decimal points.

• Hi,

Are you sure about the value you have in r1 and r2 ?

That small sample:

#include <QtDebug>

int main(int argc, char **argv)
{
double d1 = 125.0;
double d2 = 5.95;

double d3 = d1 + d2;

qDebug() << d1 << d2 << d3;

return 0;
}

shows the right values.

• Hi

there is something odd
you say
qDebug() << sql.value(0).toString(); // shows 125.00

but what comma are you then removing here?
double r1 = sql.value(0).toString().remove(",").toDouble();

• @mrjj I think his .remove(",") is in case the number has thousands separators (1,234,567.89).

• @JNBarchan
Ah yes ofc. was confused by his sample. :)

• @mrjj It's all a bit confusing :)
@Chrisw01 needs to show how he knows or outputs what is in r1, r2 and especially result, because adding 2 doubles in C will not do any rounding...

• hi Guys, sorry for the confusion, here's some better code to see what I'm talking about.

if(requireEquipmentNumber()) {
sql.exec("SELECT Expenses from EQUIPMENT where `Equipment ID` = '" + ui->expenseNumberName->currentText().toUpper() + "' LIMIT 1");
if(sql.next()) {
double currentExpenses = sql.value(0).toString().remove(",").toDouble();
qDebug() << sql.value(0).toString();
qDebug() << getVendorInvoiceAmount();
qDebug() << currentExpenses;
qDebug() << currentExpenses;
}
}

And here is the output of qDebug()

"125,499.00"
"5,995.75"
125499
5995.75
131495

I've increased the value to demonstrate the need of the removal of the comma, if you leave it in there then the toDouble() call will fail and you end up with a value of 0.

Actual total should be 131494.75. As you can see, the math is correct it is just rounding the total.

• @SGaist You are correct, and I can't find the site I found earlier, it has something to do with the QString::toDouble() function.

• It's not really clear what is the field type in the database, but in any case you should set your locale properly, i. e. matching the one set in your database, and convert the number accordingly with QLocale::toDouble. That is unless you keep your numbers in the db the same way you treat them in the code - as doubles.

• I think you will find that the reason for the rounding you are seeing is because qDebug() does not handle doubles. It handles floats, only. And I'm then guessing that (float)131494.75 == 131495. BTW, this will only affect display via qDebug(), not your code's internal use of the truly double value.

:)

• Hi all, thanks for all the input, while @JNBarchan was correct, in the code it produced accurate numbers however introduced another unique issue. if a number ended in .00 then toDouble drops it and makes it a whole number e.g. 125000.00 becomes 125000 then my parsing code would turn that into 1,250.00. I've went ahead and @kshegunov suggested and I now use QLocale::toCurrencyString() on all money input code. This seems to work rather well once I remembered to remove the "\$" from the value along with the comma's.

Thanks again for all your help..

Chris--

• @Chrisw01