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 arg() fixed number of decimal places

QString arg() fixed number of decimal places

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 10.9k Views
  • 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.
  • D Offline
    D Offline
    Dan203
    wrote on last edited by
    #1

    I'm formatting a QString with several numbers using the "%1 %2 %3" format. One of the numbers is a double and I need to print it as a number with a fixed number of decimal places. I looked at the docs for QString and the only function that takes double as input seems to return scientific notation, which I don't want. I just want to take a double that might be 59.939999991 and return 59.94

    Does anyone know how to do this?

    Previously this was an MFC string that was using Format, which uses the same syntax as sprintf. So I just used %0.3f. Just trying to get the same result in Qt.

    eyllanescE 1 Reply Last reply
    0
    • D Dan203

      I'm formatting a QString with several numbers using the "%1 %2 %3" format. One of the numbers is a double and I need to print it as a number with a fixed number of decimal places. I looked at the docs for QString and the only function that takes double as input seems to return scientific notation, which I don't want. I just want to take a double that might be 59.939999991 and return 59.94

      Does anyone know how to do this?

      Previously this was an MFC string that was using Format, which uses the same syntax as sprintf. So I just used %0.3f. Just trying to get the same result in Qt.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @Dan203 try with:

          double d = 59.939999991;
          QString value = QString("delta: %1").arg(d, 0, 'f', 3);
          qDebug() << value;
      

      Output:

      "delta: 59.940"
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      D 1 Reply Last reply
      3
      • eyllanescE eyllanesc

        @Dan203 try with:

            double d = 59.939999991;
            QString value = QString("delta: %1").arg(d, 0, 'f', 3);
            qDebug() << value;
        

        Output:

        "delta: 59.940"
        
        D Offline
        D Offline
        Dan203
        wrote on last edited by
        #3

        @eyllanesc Thank you!

        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