# Integers and rounding question

#### Ryan2003

##### New Member
How do you set the rounding for integers?
In FreeBasic, the following code:
Cerberus X:
``````#include "fbgfx.bi"
Using FB

Dim as integer  x
x = 3
Print x
x = 3.2
Print x
x = 3.8
Print x

Do
Loop Until Multikey(SC_ESCAPE)``````
Will print:
3
3
4

Iin Cerberus, the following code:
Cerberus X:
``````Function Main()
New Game()
End

Class Game Extends App

Method OnCreate()

Local x:Int

x = 3
Print x
x = 3.2
Print x
x = 3.8
Print x
End
End``````
Will print:
3
3
3

I want the last print statement to output a 4 in Cerberus.

I know about the Ceil and Floor functions, but the use of those assumes that you know ahead of time what the variable value is and how you want to change it.
And you have to use floats.
For example:
Cerberus X:
``````Import mojo

Function Main()
New Game()
End

Class Game Extends App

Method OnCreate()

Local y:Float
y = 3
Print y
y = 3.2
Print "y =" + y
Print "Ceil(y) = " + Ceil(y)
Print "Floor(y) = " + Floor(y)
y = 3.8
Print "y =" + y
Print "Ceil(y) = " + Ceil(y)
Print "Floor(y) = " + Floor(y)

End

End``````
The above code will print:
3
y =3.2
Ceil(y) = 4
Floor(y) = 3
y =3.8
Ceil(y) = 4
Floor(y) = 3

To get what I am looking for I would need to apply both functions Ceil and Floor to the variable y, but I would have to know, as in this example,
if y > 3.5 or y < 3.5. Of course this is just an example and we can see the variable's value and pick the correct function.
But, in real code, the variable could have its assignment be a complex expression e.g. y = mousex()/sin(y) * PI

Is there some way to have an integer round up to the next integer value when it is greater than half way to the next integer?

#### MikeHart

CX behaves normal here. When floats are casted to integers, the fractional part is truncated. You would need to add 0.5 to your float before casting it to an integer. I assume that PB internally does this. CX could use a rounding function that does that.

Cerberus X:
``````Function Round:Int(value:Float)
Return (value + 0.5)
End``````

#### Ryan2003

##### New Member
Thank you Mike.

• MikeHart