james
james

Reputation: 646

how to convert a float value to rounded off in iphone app

I have a float value i want to rounded off to nearest total i have find some math function but not working

Now potential profit is 300.52 so how to roundded of this please

     float potentialprofit=otherthanCereniaAnnual*totalProfit;
     NSString*potentialProfitTitle=[NSString stringWithFormat:@"%.2f",potentialprofit];

[potentialProfit setTitle:potentialProfitTitle forState:UIControlStateNormal];

Upvotes: 1

Views: 3993

Answers (2)

wangdong
wangdong

Reputation: 118

try this:

float f = ...;
f = (int)(f+0.5);

if you want to handle negative value, try this:

f = (int)(f < 0 ? f-0.5 : f+0.5);

Upvotes: 2

Prasad G
Prasad G

Reputation: 6718

int result = (int)ceilf(myFloat );
int result = (int)roundf(myFloat );
int result = (int)floor(myFloat);

float result = ceilf(myFloat );
float result = roundf(myFloat );
float result = floor(myFloat);

I think it will be helpful to you.

Upvotes: 6

Related Questions