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. Rounding up numbers
Forum Updated to NodeBB v4.3 + New Features

Rounding up numbers

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 23.0k 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.
  • F Offline
    F Offline
    fs_tigre
    wrote on last edited by
    #1

    Hi,

    I have been trying to round up some numbers but I cannot figure it out. What I have is a little program that when a button is clicked it gets information form 7 input fields and then it makes the following calculations.

    @void MainWindow::on_pushButton_Calculate_clicked()
    {
    // input fields
    QString partW = ui->lineEdit_PartWidth->text();
    QString partL = ui->lineEdit_PartLength->text();
    QString sheetW = ui->doubleSpinBox_SheetWidth->text();
    QString sheetL = ui->doubleSpinBox_SheetLength->text();
    QString clamp = ui->doubleSpinBox_Clamp->text();
    QString kerf = ui->doubleSpinBox_Kerf->text();
    QString web = ui->doubleSpinBox_Web->text();

    //converting inputs to numbers and adding some other factors
    partWidth = partW.toFloat() + kerf.toFloat();
    partLength = partL.toFloat() + kerf.toFloat();
    sheetLength = sheetL.toFloat() - (web.toFloat()* 2);
    sheetWidth = sheetW.toFloat() - (clamp.toFloat() - .5);
    
    partsY = sheetWidth / partWidth;
    partsX = sheetLength /partLength ;
    
    totalParts = partsY * partsX;
    
    ui->label_TotalParts->setText(QString::number(totalParts));
    

    }@

    Everything is working as it should the only thing is that I want to round up partsY and partsX so that when they are multiplied there are no decimal numbers.

    I tried modifying the following line...
    @totalParts = round(partsY) * round(partsX);@
    but it doesn't work.

    I know it may be hard to know whats wrong without seeing the actual code but can someone give it a shot?

    Thanks a lot for your help.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      The rounding method is called "qRound":http://qt-project.org/doc/qt-4.8/qtglobal.html#qRound .

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

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        I would suggest to round after the multiplication. That is more accurate. qRound is the way to go, so this should work:
        @totalParts = qRound(partsY * partsX);@
        If you don't want to use the QRound the basic way is to have a float value, add (positive value) or subtract 0,5 (negative value) and typeconvert into a int.
        @if(partsY * partsX < 0)
        iTotalParts = (int)((partsY * partsX) - 0,5);
        else
        iTotalParts = (int)((partsY * partsX) + 0,5);@

        Greetz

        Greetz, Jeroen

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fs_tigre
          wrote on last edited by
          #4

          I was trying to use qRound when I realized that this could be just a metter of changing my variable type from float to int in fact it did the trick.

          Thank you all very much for your help.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Note that changing the variable type will not result in "natural" rounding. It will result in flooring values. So, 2.9999 will be "rounded" to 2, not 3.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fs_tigre
              wrote on last edited by
              #6

              That is actually what I want. But thanks a lot for the clarification.

              I love this forum!

              Thanks a lot

              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