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. QString to Double conversion problem
Forum Updated to NodeBB v4.3 + New Features

QString to Double conversion problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.6k 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.
  • R Offline
    R Offline
    Raagini
    wrote on last edited by
    #1

    I'm a new bie in qt. I have a problem while converting a string value to double. My string value is

    @QString str = 100.94519;
    double value = str.toDouble();
    @

    My Output while converting it to double is 100.95.
    The problem is the double value is converting to 2 decimals. But i want the same string value to be converted as a double value. How can i convert a string value to a double value?

    1 Reply Last reply
    1
    • M Offline
      M Offline
      maisoui
      wrote on last edited by
      #2

      Hi,

      You should have a look to "QString documentation":http://qt-project.org/doc/qt-4.8/qstring.html#number-2 and use the method QString::number.

      Regards,
      Jonathan

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        [quote author="maisoui" date="1379937149"]Hi,

        You should have a look to "QString documentation":http://qt-project.org/doc/qt-4.8/qstring.html#number-2 and use the method QString::number.

        Regards,
        Jonathan
        [/quote]

        While you should read the post thoroughly :P The OP is asking for a conversion from QString to double, not the other way around.

        (Z(:^

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Raagini try with QLocale::toDouble().

          (Z(:^

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maisoui
            wrote on last edited by
            #5

            Are you sure the value in your string is correct? That's why I talked about QString::number.

            For me this line is not correct : QString str = 100.94519; (Which version of Qt do you use?)

            Regards

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Ah right, maisoui my bad, apologies.

              However, I suspect this is just a quick example and the actual code in Raagini's project is different.

              (Z(:^

              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