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. [Solved] Precision of qDebug()?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Precision of qDebug()?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 16.5k Views 1 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
    CAD_coding
    wrote on 2 May 2013, 06:52 last edited by
    #1

    Hi,
    I have a double variable (say x) with a value like 0.123456789e-10. When I print the value of x via qDebug(), it is showing only part of the value.
    qDebug() << x << endl;
    So, this prints 0.1234e-10. I want it to print 0.123456789e-10. How can I do that?
    Similarly if x = 0.0000012345. It prints 1.234e-6, but I want it to print 0.0000012345. How can I do that?
    I have installed a messagehandler so that the output from qDebug() is printed to a file.
    Thank You.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chris17
      wrote on 2 May 2013, 07:16 last edited by
      #2

      you can use QString::number to specify the format

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CAD_coding
        wrote on 2 May 2013, 07:23 last edited by
        #3

        Yes but isn't that a bit tedious to, convert a double to a string every time I have to just print its value with better precision?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chris17
          wrote on 2 May 2013, 07:59 last edited by
          #4

          you could also write your own logger class to use instead of qdebug

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on 2 May 2013, 08:04 last edited by
            #5

            Try something like this:

            @
            qDebug() << qSetRealNumberPrecision( 10 ) << "x = " << x;
            @

            There are a whole bunch of ways to configure the QTextStream object used by qDebug(). See for example http://qt-project.org/doc/qt-5.0/qtcore/qtextstream.html#qSetRealNumberPrecision and http://qt-project.org/doc/qt-5.0/qtcore/qtextstream.html#qtextstream-manipulators

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            1
            • C Offline
              C Offline
              CAD_coding
              wrote on 2 May 2013, 08:48 last edited by
              #6

              Hi ZapB,
              Thanks for that!
              I will try that & let you know if there is any problem.

              @chris17, I will keep that in mind for my next project. Thanks.

              UPDATE: Hi ZapB,
              Since I have used my messagehandler for qDebug() to print to a file, is it possible to do something so that the text stream for my messagehandler can be set with the realnumberprecision property only once at startup so that I dont have to add the code you showed every time I am using qDebug()?

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZapB
                wrote on 2 May 2013, 09:45 last edited by
                #7

                Hmm, I'm not aware of any simple way to do this. You could write yoru own wrapper function around qDebug() << qRealNumberPrecision() and call that instead of qDebug. Or even a simple macro perhaps.

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply
                0

                1/7

                2 May 2013, 06:52

                • Login

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