Rounding up numbers
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
- //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;
- }
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.
5 replies
The rounding method is called qRound [qt-project.org] .
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
You must log in to post a reply. Not a member yet? Register here!



