# Rounding a float and casting to an int

This is a discussion on Rounding a float and casting to an int within the C Programming forums, part of the General Programming Boards category; Is this the right way to do this? Code: int input; float temp = input * (2/3); int output = ...

1. ## Rounding a float and casting to an int

Is this the right way to do this?

Code:
```int input;

float temp = input * (2/3);

int output = roundf(temp);```
Thank you

2. Assuming that you actually give input a value, the problem would be that (2/3) results in integer division which truncates the result to 0. If you change it to (2.0f/3.0f) it should work. However, I wonder if this would be simpler yet give identical results:
Code:
`int output = (input * 2) / 3;`

3. Ah, yes. thank you.
So, for
int output = (input * 2) / 3;
Is it truncating or rounding?

4. Originally Posted by pollypocket4eva
So, for
int output = (input * 2) / 3;
Is it truncating or rounding?
Ah yes, that would truncate, not round.

5. For my purpose, either is fine. But was curious for future reference.
Thank you