tags:

views:

272

answers:

3

Hi Guys,

I need to round a float value and convert it into NSInteger value.

for example:

       float f = 90.909088;

       I want the result as 91.

Can any one help to get rid of this.

Anyone's help will be much appreciated.

+1  A: 

try doing 'f = floor(f+0.5)' before the integer conversion

Tom Womack
when I done with this if I pass the value as 315 it was converted as 32.I need to remain it same as 315 if it doesn't have any decimal points.
monish
A: 

Try:

float f = 90.909088;
NSNumber *myNumber = [NSNumber numberWithDouble:(f+0.5)];
NSInteger myInt = [myNumber intValue];
pheelicks
when I done with this if I pass the value as 315 it was converted as 32.I need to remain it same as 315 if it doesn't have any decimal points
monish
+1  A: 

One of the following C math functions might work for you:

  • double ceil(double)
  • double floor(double)
  • double nearbyint(double)
  • double rint(double)
  • double round(double)
  • long int lrint(double)
  • long int lround(double)
  • long long int llrint(double)
  • long long int llround(double)
  • double trunc(double)

To get more documentation, open a terminal session and type (for example)

man lround

I pick lround as an example because I think that is the one you want.

JeremyP