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 formatting for rounding a decimal number
Forum Updated to NodeBB v4.3 + New Features

QString formatting for rounding a decimal number

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 665 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.
  • S Offline
    S Offline
    Swati777999
    wrote on 7 Mar 2022, 07:19 last edited by
    #1

    I've following code for rounding myVal to 6 decimal places i.e 0.314568

     QString myVal ="0.3145678";
      qDebug()<<"Ans=" << QString::asprintf("%10.6f",myVal);
    

    It says no member named 'asprintf' in 'Qstring' . Can someone help me how to do it?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 7 Mar 2022, 07:46 last edited by ChrisW67 3 Aug 2022, 02:41
      #2

      QString::asprintf()

      However, the format specifier "%10.6f" requires a numeric value argument (usually a float or double) and you are feeding it a string. Your line compiles with a warning (at least with my compiler) and does something undefined as a result

      There is a difference between rounding a numeric value at 6 decimal places, and simply displaying it in human readable form rounded to 6 decimal places. Which do you want?

      #include <QCoreApplication>
      #include <QDebug>
      #include <QString>
      #include <cmath>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QString myVal ="0.3145678";
      
          double value = myVal.toDouble();
      
      
          qDebug()<<"Original string =" << myVal;
          qDebug()<<"Original value  =" << QString::asprintf("%10.7f", value);
          qDebug()<<"Rounded display =" << QString::asprintf("%10.6f", value);
      
          // Round the actual value
          value = std::round(value * 1e6) / 1e6;
          qDebug()<<"Rounded value   =" << QString::asprintf("%10.7f", value);
          qDebug()<<"Rounded display =" << QString::asprintf("%10.6f", value);
      
          // Your line
          qDebug()<<"Ans=" << QString::asprintf("%10.6f",myVal);
      
          return 0;
      }
      

      The warning

      ../untitled/main.cpp: In function ‘int main(int, char**)’:
      ../untitled/main.cpp:25:49: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘QString’ [-Wformat=]
           25 |     qDebug()<<"Ans=" << QString::asprintf("%10.6f",myVal);
              |                                            ~~~~~^
              |                                                 |
              |                                                 double
      

      Output

      Original string = "0.3145678"
      Original value  = " 0.3145678"
      Rounded display = "  0.314568"
      Rounded value   = " 0.3145680"
      Rounded display = "  0.314568"
      Ans= "  0.000000"
      
      1 Reply Last reply
      2

      1/2

      7 Mar 2022, 07:19

      • Login

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