29

I have one value 100023 and I have taken it in NSString.

Now I want to pass this value in my web service which contains long parameter type so how can I convert string value to long.

Robin
  • 10,011
  • 5
  • 49
  • 75
Ankit Vyas
  • 7,507
  • 13
  • 56
  • 89

6 Answers6

59

You can use NSString methods intValue longLongValue.

YPK
  • 1,851
  • 18
  • 18
24

For a small number like this "100023", this is acceptable with 'longlongvalue'. However, if the number digits have more than >10 in which commonly regard as the use case for long value. Then, you will run in into this problem, such as:

String value "86200054340607012013"

do

@"86200054340607012013" integerValue or intValue 

you will produce this in the print statement

2147483647 

if you do

@"86200054340607012013" longlongvalue

you will produce this in the print statement

9223372036854775807

This works for me and print out the expected number.

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:@"2394739287439284734723"];
NSLog(@"longlong: %llu", [myNumber longLongValue]);   
Kevin
  • 828
  • 11
  • 14
14

Use this:

    yourLong = [yourString longLongValue];
Andrés Canavesi
  • 2,164
  • 20
  • 21
2

you can use the doubleValue Method to avoid lose of precision warnings

Chuy47
  • 2,391
  • 1
  • 30
  • 29
0

The answer is :

float floatId = [strID floatValue];
mjs
  • 21,431
  • 31
  • 118
  • 200
-12

Do this...

long value = [myString longValue]
Shadrax
  • 57
  • 1
  • 9
  • 1
    Don't do this because this cause the exeption: "unrecognized selector sent to instance". Use "- (long long)longLongValue;" or "- (int)intValue;" instead. – e40pud Mar 31 '11 at 07:06