0

Possible Duplicate:
Objective-c: How to round off Float values?

I wanted to roundoff the float value from 42.56789 to 42.6. Can any one help me out in this.

Thanks in advance

Community
  • 1
  • 1
Malathi
  • 51
  • 1
  • 2

4 Answers4

0

There are two functions in c namely floor and ceil to roundoff the float values.

Refer Wikipedia reference

karthik
  • 17,453
  • 70
  • 78
  • 122
0
float value=42.56789;
NSString *roundedValue=[NSString stringWithFormat:@"%0.f",value];
NSLog(@"%@",roundedValue);

It prints 43 on console.

Gypsa
  • 11,230
  • 6
  • 44
  • 82
0
float f = 42.56789;

    f *= 10.0f;

    f = ceilf(f);

    f /= 10.0f;

    NSLog(@"%.02f",f);
saadnib
  • 11,145
  • 2
  • 33
  • 54
0

I have done it manually .

float val=42.567890// float gives 6 decimal places
int x=(int) val;
float f=val-(int)val;
int o=(int) f*10;
float round=x+o/10+(f-(o/10))*1000)/50;
thkala
  • 84,049
  • 23
  • 157
  • 201
Samarth2011
  • 1,343
  • 2
  • 11
  • 13
  • you should not type cast a float value into int value because float has a higher rage then int. – saadnib May 06 '11 at 06:06