April 8, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

Rounding up numbers



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.

  1. void MainWindow::on_pushButton_Calculate_clicked()
  2. {
  3.      // input fields
  4.     QString partW = ui->lineEdit_PartWidth->text();
  5.     QString partL = ui->lineEdit_PartLength->text();
  6.     QString sheetW = ui->doubleSpinBox_SheetWidth->text();
  7.     QString sheetL = ui->doubleSpinBox_SheetLength->text();
  8.     QString clamp = ui->doubleSpinBox_Clamp->text();
  9.     QString kerf = ui->doubleSpinBox_Kerf->text();
  10.     QString web = ui->doubleSpinBox_Web->text();
  12.     //converting inputs to numbers and adding some other factors
  13.     partWidth = partW.toFloat() + kerf.toFloat();
  14.     partLength = partL.toFloat() + kerf.toFloat();
  15.     sheetLength = sheetL.toFloat() - (web.toFloat()* 2);
  16.     sheetWidth = sheetW.toFloat() - (clamp.toFloat() - .5);
  18.     partsY = sheetWidth / partWidth;
  19.     partsX = sheetLength /partLength ;
  21.     totalParts = partsY * partsX;
  23.     ui->label_TotalParts->setText(QString::number(totalParts));
  24. }

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…

  1. 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.

5 replies

April 8, 2012

koahnig koahnig
Area 51 Engineer
2877 posts

The rounding method is called qRound [qt-project.org] .

April 8, 2012

Jeroentje@home Jeroentje@ho..
Dinosaur Breeder
730 posts

I would suggest to round after the multiplication. That is more accurate. qRound is the way to go, so this should work:

  1. 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.
  1. if(partsY * partsX < 0)
  2.    iTotalParts = (int)((partsY * partsX) - 0,5);
  3. else
  4.    iTotalParts = (int)((partsY * partsX) + 0,5);



Greetz, Jeroen

April 8, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

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.

April 9, 2012

Andre Andre
Robot Herder
6295 posts

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.


Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 9, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

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

I love this forum!

Thanks a lot

  ‹‹ Emiting signal does not work      No way to create localized list of country names? ››

You must log in to post a reply. Not a member yet? Register here!