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
  • 41
  • 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,006
  • 70
  • 76
  • 122
0
float value=42.56789;
NSString *roundedValue=[NSString stringWithFormat:@"%0.f",value];
NSLog(@"%@",roundedValue);

It prints 43 on console.

Gypsa
  • 11,180
  • 6
  • 43
  • 82
0
float f = 42.56789;

    f *= 10.0f;

    f = ceilf(f);

    f /= 10.0f;

    NSLog(@"%.02f",f);
saadnib
  • 11,115
  • 2
  • 32
  • 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
  • 80,583
  • 22
  • 150
  • 196
Samarth2011
  • 1,333
  • 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