开发者

How to round of the value? [duplicate]

This ques开发者_开发问答tion already has answers here: Closed 11 years ago.

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜