# Thread: need help coverting float to integer

1. ## need help coverting float to integer

Im writing a program for a course in school. We need to round our years of service. The user inputs a value that is float, so they can enter 28.3 or 28.6 etc... We need to round the value so we can output it as an integer value. so 28.3 needs to be 28 and 28.6 needs to be 29. Can anyone tell me how to do this conversion???

2. Try this round function.
Code:
```#include <iostream>
#include <cmath>

using namespace std;

double round( double num );

int main(void)
{
double a = 28.3;
double b = 28.6;

cout << round( a ) << endl;
cout << round( b ) << endl;

return 0;
}

double round( double num )
{
return floor(num + 0.5);
}```

3. Thank you so much swoopy!

4. It's worth pointing out, though, that the whole point of learning a programming language is so that you can use the building blocks provided by that language to create your own solutions to problems. The question you posed was trivial to solve, even for a beginner, with just a little bit of effort, as long as you understand what 'rounding' means. If you're unable to even attempt to solve such a trivial problem you may need to rethink learning to program at all.

Unfortunately, you missed an opportunity to develop some useful problem solving skills this time round.

;-)

5. you know i dont plan on programming for a career. i missed a couple classes already because i play for the college soccer team. i needed a little help, is that too much to ask?

Popular pages Recent additions