-1
NSString * addString=[arrayyyy componentsJoinedByString:@","];

NSLog(@"add string is: %@",addString);// result is: 45,1

Now I want to convert above string into integer.

I have tried this:

NSInteger myInt=[addString intValue]; 
//NSLog(@"myInt is: %d",myInt);// result is: 45
IOS Praveen
  • 31
  • 2
  • 6

3 Answers3

2

If you expected 45.1 then there are two things wrong :

  1. 45.1 is not an integer. You would have to use floatValue to read the value.

  2. 45,1 (notice the comma) is not a valid float number. While 45,1 is valid in some locale (i.e. in french its 1 000,25 instead of 1,000.25) you would have to convert the string with an NSNumberFormatter before reading the floatValue.

.

// Can't compile and verify this right now, so please bear with me.
NSString *str = @"45,1";
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease]; // lets say French from France
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setLocale:locale];
float value = [[formatter numberFromString:str] floatValue];   // value = 45.1
HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • Thank you for your response, but i want same result as an integer format, like 45,1, Please help me – IOS Praveen Mar 09 '12 at 14:27
  • 1
    `45.1` or `45,1` are **decimal** values. It's impossible to store them into an **integer**. Integers are whole values. The only way to put a decimal value into an integer is by rounding the value : it will result into `45`. –  Mar 09 '12 at 15:07
0

From reading the question a lot, I think I may understand what you want.

The starting point seems to be:

NSLog(@"add string is: %@",addString);// result is: 45,1

And the current ending point is:

NSLog(@"myInt is: %d",myInt);// result is: 45

But it seems that you still want to print out 45,1

My guess on this is that you have an array of 2 strings [@"45",@"1"] called arrayyyy and you want to print out both values as integers. If this is so then what I think you want is:

NSInteger myInt1 = [[arrayyyy objectAtIndex:0] intValue];
NSInteger myInt2 = [[arrayyyy objectAtIndex:1] intValue];
NSLog(@"add string is: %d,%d",myInt1,myInt2);

Note This will crash horribly with an NSRangeException if there are not at least two strings in the array. So at the very least you should do:

NSInteger myInt1 = -1;
NSInteger myInt2 = -1;
if ([arrayyyy length] >0) myInt1 = [[arrayyyy objectAtIndex:0] intValue];
if ([arrayyyy length] >1) myInt2 = [[arrayyyy objectAtIndex:1] intValue];
NSLog(@"add string is: %d,%d",myInt1,myInt2);

But even this is bad as it assumes that the guard value of -1 will not be present in the actual data.

Peter M
  • 7,309
  • 3
  • 50
  • 91
0

Try out NSExpression which works with mathematical symbols too (i.e. +, -, /, *):

NSNumber *numberValue = [[NSExpression expressionWithFormat:inputString] expressionValueWithObject:nil context:nil];

// do something with numberValue
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201