Reputation: 137
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
Upvotes: 0
Views: 255
Reputation: 163
NSString* str = @"2.61111111";
double value = [str doubleValue];
2.5 -> 3: int num = value+0.5;
2.6 -> 3: int num = value+0.4;
Set as your need:
double factor = 0.4
if (value < 0) value *= -1;
int num = value+factor;
NSLog(@"%d",num);
Upvotes: 0
Reputation: 11587
You can round off decimal values by using NSNumberFormatter There are some examples you can go through:
NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
[format setPositiveFormat:@"0.##"];
NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.342]]);
NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.3]]);
NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.0]]);
Corresponding results:
2010-08-22 15:04:10.614 a.out[6954:903] 25.34
2010-08-22 15:04:10.616 a.out[6954:903] 25.3
2010-08-22 15:04:10.617 a.out[6954:903] 25
Upvotes: 0
Reputation: 27448
Try below code I have tested it and work for every digits,
NSString *str = @"2.7";
NSArray *arr = [str componentsSeparatedByString:@"."];
NSString *firstDigit = [arr objectAtIndex:0];
NSString *secondDigit = [arr objectAtIndex:1];
if (secondDigit.length > 1) {
secondDigit = [secondDigit substringFromIndex:1];
}
int secondDigitIntValue = [secondDigit intValue];
int firstDigitIntValue = [firstDigit intValue];
if (secondDigitIntValue > 5) {
firstDigitIntValue = firstDigitIntValue + 1;
}
NSLog(@"final result : %d",firstDigitIntValue);
Or another solution - little bit short
NSString *str1 = @"2.444";
float my = [str1 floatValue];
NSString *resultString = [NSString stringWithFormat:@"%.f",my]; // if want result in string
NSLog(@"%@",resultString);
int resultInInt = [resultString intValue]; //if want result in integer
Upvotes: 1
Reputation: 479
NSString *value = @"1.23456";
float floatvalue = value.floatValue;
int rounded = roundf(floatvalue);
NSLog(@"%d",rounded);
if you what the round with greater value please use ceil(floatvalue)
if you what the round with lesser value please use floor(floatvalue)
Upvotes: 0
Reputation: 5436
To round value to the nearest integer use roundf()
function of math
.
import math.h
first:
#import "math.h"
Example,
float ValueToRoundPositive;
ValueToRoundPositive = 8.4;
int RoundedValue = (int)roundf(ValueToRoundPositive); //Output: 8
NSLog(@"roundf(%f) = %d", ValueToRoundPositive, RoundedValue);
float ValueToRoundNegative;
ValueToRoundNegative = -6.49;
int RoundedValueNegative = (int)roundf(ValueToRoundNegative); //Output: -6
NSLog(@"roundf(%f) = %d", ValueToRoundNegative, RoundedValueNegative);
Read doc here for more information:
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/roundf.3.html
Upvotes: 0
Reputation: 2786
plz use this
lblHours.text =[NSString stringWithFormat:@"%.02f", [yourstrvalue doubleValue]];
update
NSString *a =@"2.67899";
NSString *b =[NSString stringWithFormat:@"%.01f", [a doubleValue]];
// b will contane only one vlue after decimal
NSArray *array = [b componentsSeparatedByString:@"."];
int yourRating;
if ([[array lastObject] integerValue] > 5) {
yourRating = [[array firstObject] intValue]+1;
}
else
{
yourRating = [[array firstObject] intValue];
}
NSLog(@"%d",yourRating);
Upvotes: 1
Reputation: 7903
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);
Upvotes: 1
Reputation: 3573
check this
float floatVal = 2.6111;
long roundedVal = lroundf(floatVal);
NSLog(@"%ld",roundedVal);
Upvotes: 1
Reputation: 2777
Try This
float f=2.6;
NSLog(@"%.f",f);
Hope this helps.
Upvotes: 2