Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT Currency Issue
Qt 6.11 is out! See what's new in the release blog

QT Currency Issue

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 5.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chrisw01
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chrisw01
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            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();

            JonBJ 1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              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();

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              mrjjM 1 Reply Last reply
              1
              • JonBJ JonB

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Chrisw01
                    wrote on last edited by Chrisw01
                    #9

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

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

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      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.

                      C Offline
                      C Offline
                      Chrisw01
                      wrote on last edited by
                      #10

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

                      kshegunovK 1 Reply Last reply
                      0
                      • C Chrisw01

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

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #11

                        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.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        2
                        • JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          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

                          :)

                          1 Reply Last reply
                          1
                          • C Offline
                            C Offline
                            Chrisw01
                            wrote on last edited by
                            #13

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

                            JonBJ 1 Reply Last reply
                            1
                            • C Chrisw01

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

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved