user6438311 user6438311 - 3 months ago 8
iOS Question

How to convert this value into absolute value?

I am getting this from webservice
"rateavg": "2.6111"

now i am getting this in a string.
How to do this that if it is coming 2.6 it will show 3 and if it will come 2.4 or 2.5 it will show 2 ?

How to get this i am not getting. please help me

Answer

I come up with this, a replica of your query:

NSString* str = @"2.611";
double duble = [str floatValue];

NSInteger final = 0;


if (duble > 2.5) {
    final = ceil(duble);
}else{
    final = floor(duble);
}

NSLog(@"%ld",(long)final);

So it a case of using either ceil or floor methods.

Edit: Since you want it for all doubles:

NSString* str = @"4.6";
double duble = [str floatValue];

NSInteger final = 0;

NSInteger temp = floor(duble);
double remainder = duble - temp;

if (remainder > 0.5) {
    final = ceil(duble);
}else{
    final = floor(duble);
}

NSLog(@"%ld",(long)final);