Ankit Vyas
Ankit Vyas

Reputation: 7501

How i can convert NSString to long value?

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.

Upvotes: 29

Views: 38789

Answers (6)

Chuy47
Chuy47

Reputation: 2417

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

Upvotes: 2

Kevin
Kevin

Reputation: 838

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]);   

Upvotes: 25

mjs
mjs

Reputation: 22347

The answer is :

float floatId = [strID floatValue];

Upvotes: 0

Andrés Canavesi
Andrés Canavesi

Reputation: 2174

Use this:

    yourLong = [yourString longLongValue];

Upvotes: 14

Shadrax
Shadrax

Reputation: 57

Do this...

long value = [myString longValue]

Upvotes: -12

YPK
YPK

Reputation: 1851

You can use NSString methods intValue longLongValue.

Upvotes: 59

Related Questions