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
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
There are two functions in c namely floor and ceil to roundoff the float values.
Refer Wikipedia reference
float value=42.56789;
NSString *roundedValue=[NSString stringWithFormat:@"%0.f",value];
NSLog(@"%@",roundedValue);
It prints 43 on console.
float f = 42.56789;
f *= 10.0f;
f = ceilf(f);
f /= 10.0f;
NSLog(@"%.02f",f);
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;