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. toDouble() lost the data
Forum Updated to NodeBB v4.3 + New Features

toDouble() lost the data

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.3k Views 2 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.
  • I Offline
    I Offline
    iskenderoguz
    wrote on last edited by
    #1

    I get data from database with this

    query.value(0).toString();
    

    after that I convert string to double

    query.value(0).toString().toDouble();
    

    But I lost my data

    Original data is 123.45
    but after toDouble() data is 123.5

    How can I fix this ?

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

      Hi,

      Are you sure that your string contains 123.45 ?

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        iskenderoguz
        wrote on last edited by
        #3

        yes I am sure

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

          Just tested with:

          QString test("123.45");
          qDebug() << test.toDouble() << test.toFloat();
          

          And got the right results.

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            iskenderoguz
            wrote on last edited by
            #5

            yes your code work but my problem still exists

            
            qDebug()<<query.value(0).toString()<<" - "<<query.value(0).toDouble()<<" - "<<query.value(0).toString().toDouble();
            
            "123.45" - 123.5 - 123.5
            
            1 Reply Last reply
            0
            • I Offline
              I Offline
              iskenderoguz
              wrote on last edited by
              #6

              Also I tested this

              QString test(query.value(0).toString());
              qDebug() << test << test.toDouble() << test.toFloat();
              
              "123.45" 123.5 123.5
              
              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #7

                You did not lose any data. qDebug() simply displays 4 significant figures by default.

                Test this:

                qDebug() << (test.toDouble() - 123.5) // Should produce -0.05
                

                Also, you can ask it to display more significant figures:

                qDebug() <<  qSetRealNumberPrecision(10) << test << test.toDouble() << test.toFloat();
                

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                1

                • Login

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