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. [Solved] Modulo operator %
Forum Updated to NodeBB v4.3 + New Features

[Solved] Modulo operator %

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

    Hi

    Following sample from standard C:
    where for instance a = 1234 (value) and b = 10000000 (scaling factor)
    printf("value: %d.%6.6d\n", ( a / b ), ( a % b ));
    would return: value: 0.001234

    I now tried to do something similar in Qt:

    ui->Label->setText(tr("value: %1.%2")
    .arg(data.section(' ', 7, 7).toUInt(&ok, 10) / data.section(' ', 8, 8).toUInt(&ok, 10))
    .arg(data.section(' ', 7, 7).toUInt(&ok, 10) % data.section(' ', 8, 8).toUInt(&ok, 10), 7));
    

    which leads me to
    value: 0. 1234
    and I can not manage to have it filled with '0' instead of ' '.

    What am I doing wrong?
    Thanks

    K 1 Reply Last reply
    0
    • McLionM McLion

      Hi

      Following sample from standard C:
      where for instance a = 1234 (value) and b = 10000000 (scaling factor)
      printf("value: %d.%6.6d\n", ( a / b ), ( a % b ));
      would return: value: 0.001234

      I now tried to do something similar in Qt:

      ui->Label->setText(tr("value: %1.%2")
      .arg(data.section(' ', 7, 7).toUInt(&ok, 10) / data.section(' ', 8, 8).toUInt(&ok, 10))
      .arg(data.section(' ', 7, 7).toUInt(&ok, 10) % data.section(' ', 8, 8).toUInt(&ok, 10), 7));
      

      which leads me to
      value: 0. 1234
      and I can not manage to have it filled with '0' instead of ' '.

      What am I doing wrong?
      Thanks

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @McLion
      One way I can think off is using the QTextStream::setPadChar. You have to write to a string there.
      Personally I would prefer using std::ostringstream, but basically do the same.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        I might be missing something but why not just do this?

        QString::number((double)a/b, 'f', 6);
        

        Btw. 1234/10000000 is 0.0001234 not 0.001234 so your padding in the first example is flawed.

        1 Reply Last reply
        0
        • McLionM Offline
          McLionM Offline
          McLion
          wrote on last edited by McLion
          #4

          Thank you guys.
          I got it solved. I extended the .arg() with the padding which in the first place did not work because I forgot the QChar casting. Here's how it works:

          ui->label->setText(tr("BER: %1.%2")
          .arg(data.section(' ', 7, 7).toUInt(&ok, 10) / data.section(' ', 8, 8).toUInt(&ok, 10))
          .arg(data.section(' ', 7, 7).toUInt(&ok, 10) % data.section(' ', 8, 8).toUInt(&ok, 10), 6, 10, QChar('0')));
          

          @Chris-Kawa:
          You're right. 6 is correct ... typo.

          btw: I still could not figure how to mark as code in the new forum ..

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @McLion To post code surround it with backticks i.e. ```your code here``` for a code block or `your code` for inline code.

            1 Reply Last reply
            1
            • McLionM Offline
              McLionM Offline
              McLion
              wrote on last edited by
              #6

              Thanks.
              I tried with the forward and the straight tick but missed to try with the backward.

              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