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


  • Qt Champions 2016

    @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.

  • Lifetime Qt Champion


    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.

  • Qt Champions 2016

    Adding to @SGaist (wondering about what you have in the variants)

    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).

  • Qt Champions 2016

    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();
                   double addedExpenses = getVendorInvoiceAmount().remove(",").toDouble();
                   qDebug() << sql.value(0).toString();
                   qDebug() << getVendorInvoiceAmount();
                   qDebug() << currentExpenses;
                   qDebug() << addedExpenses;
                   currentExpenses += addedExpenses;
                   qDebug() << currentExpenses;

    And here is the output of qDebug()


    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.

  • Qt Champions 2016

    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.

    See https://stackoverflow.com/questions/39146527/can-you-set-the-qdebug-floating-point-precision-and-number-format-globally and https://forum.qt.io/topic/26810/solved-precision-of-qdebug/2


  • 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..


  • @Chrisw01
    I am glad if my input helped you resolve your problem.

    However, at least for the benefit of anyone else reading this, let's be clear about one thing:

    if a number ended in .00 then toDouble drops it and makes it a whole number e.g. 125000.00 becomes 125000

    Whatever toDouble() you are talking about, it returns a double type. As a number/double 125000.00 == 125000, they are the same. There is no "dropping" of anything.

    What you must mean, I think, is something like:

    When I convert a double number to a string and then parse it (for whatever reason), if the number is a whole number the string comes out without any decimal places but my parsing code assumes it has 2 decimal places and so returns the wrong result (e.g. 125000 -> 1250.00 in the above case).

Log in to reply

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