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. Replacing QString().sprintf with arg calls for double
Forum Updated to NodeBB v4.3 + New Features

Replacing QString().sprintf with arg calls for double

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.2k 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.
  • J Offline
    J Offline
    Johan Borkhuis
    wrote on last edited by
    #1

    Hello,

    I am trying to update my code from using sprintf to arg, as sprintf is deprecated and asprintf is not advised for new code.
    But I am getting some issues with converting doubles to strings. With arg you can specify precision and a fillchar, but with doubles this fillchar is also applied to the front

    This is my example code:

    int main(int argc, char *argv[]) {
      QTextStream output(stdout);
    
      output << QString().sprintf("%12.6lf", 123.456) << endl;
      output << QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')) << endl;
    
      return 0;
    }
    

    This results in the following output:

      123.456000
    00123.456000
    

    Is it possible to change this behaviour of QString(..).arg(..)?

    Regards,
    Johan

    M Christian EhrlicherC 2 Replies Last reply
    0
    • J Johan Borkhuis

      Hello,

      I am trying to update my code from using sprintf to arg, as sprintf is deprecated and asprintf is not advised for new code.
      But I am getting some issues with converting doubles to strings. With arg you can specify precision and a fillchar, but with doubles this fillchar is also applied to the front

      This is my example code:

      int main(int argc, char *argv[]) {
        QTextStream output(stdout);
      
        output << QString().sprintf("%12.6lf", 123.456) << endl;
        output << QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')) << endl;
      
        return 0;
      }
      

      This results in the following output:

        123.456000
      00123.456000
      

      Is it possible to change this behaviour of QString(..).arg(..)?

      Regards,
      Johan

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @Johan-Borkhuis

      qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' '));
      qDebug()<<QString::asprintf("% 12.6lf", 123.456);
      "  123.456000"
      "  123.456000"
      

      or

      qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0'));
      qDebug()<<QString::asprintf("%012.6lf", 123.456);
      "00123.456000"
      "00123.456000"
      
      JonBJ 1 Reply Last reply
      3
      • J Johan Borkhuis

        Hello,

        I am trying to update my code from using sprintf to arg, as sprintf is deprecated and asprintf is not advised for new code.
        But I am getting some issues with converting doubles to strings. With arg you can specify precision and a fillchar, but with doubles this fillchar is also applied to the front

        This is my example code:

        int main(int argc, char *argv[]) {
          QTextStream output(stdout);
        
          output << QString().sprintf("%12.6lf", 123.456) << endl;
          output << QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')) << endl;
        
          return 0;
        }
        

        This results in the following output:

          123.456000
        00123.456000
        

        Is it possible to change this behaviour of QString(..).arg(..)?

        Regards,
        Johan

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M mpergand

          @Johan-Borkhuis

          qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' '));
          qDebug()<<QString::asprintf("% 12.6lf", 123.456);
          "  123.456000"
          "  123.456000"
          

          or

          qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0'));
          qDebug()<<QString::asprintf("%012.6lf", 123.456);
          "00123.456000"
          "00123.456000"
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @mpergand said in Replacing QString().sprintf with arg calls for double:

          qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' '));

          This is what the OP wants. I believe [can't test] u' ' is the default so can presumably be omitted:

          QString("%1").arg(123.456, 12, 'f', 6);
          

          ?

          M 1 Reply Last reply
          1
          • JonBJ JonB

            @mpergand said in Replacing QString().sprintf with arg calls for double:

            qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' '));

            This is what the OP wants. I believe [can't test] u' ' is the default so can presumably be omitted:

            QString("%1").arg(123.456, 12, 'f', 6);
            

            ?

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            @JonB said in Replacing QString().sprintf with arg calls for double:

            ' ' is the default so can presumably be omitted:

            Yes but "ça va mieux en le disant" :)

            JonBJ 1 Reply Last reply
            1
            • M mpergand

              @JonB said in Replacing QString().sprintf with arg calls for double:

              ' ' is the default so can presumably be omitted:

              Yes but "ça va mieux en le disant" :)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @mpergand Yep, it was for my own understanding that it is the default behaviour.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Johan Borkhuis
                wrote on last edited by
                #7

                Thank you all for this. I was expecting that I needed the '0' fillchar to fill at the end as well, but I was clearly wrong there. This helped me a lot.

                1 Reply Last reply
                0
                • J Johan Borkhuis has marked this topic as solved on

                • Login

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