I am trying to develop a code that will take a number similar to 8.23 or any number with a decimal and convert it to a whole number like 8. I would like to do this in an if/then statement but i dont know which process to call it from.

Recommended Answers

All 3 Replies

Can't you just use the following:

float frac = 8.23;
int integer = frac + 0.5;

but its for any number with a decimal number. by adding basicall i want to identify the deicmal and if it is greater than or equal to 5 the number increases by one, less than 5 it remains..

this works for any decimal:

int round(double x){
   return (int)(x+.5);
   }

if you really want to use if/then, someting like:

int round(double x){
   double dec = x - (int)x;

   if(dec >= 0.5)
      return 1 + (int)x;
   else
      return (int)x;
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.