8

I have long integer value ( ex: 2705758126 ) in NSString.

When i try to show it: NSLog(@"%i", [myValue integerValue]); it return: 2147483647.

How to show, compare and etc. this long integer

4 Answers 4

23

Try with

NSLog(@"%lld", [myValue longlongValue]);
Sign up to request clarification or add additional context in comments.

1 Comment

I made an edit to fix the case in the method. I had to add a link because edits of less than 6 characters are not allowed... (That is very annoying, and has bitten me on more than one occasion.)
4

The documentation recommends using @"%ld" and @"%lu" for NSInteger and NSUInteger, respectively. Since you're using the integerValue method (as opposed to intValue or longLongValue or whatever), that will be returning an NSInteger.

6 Comments

@IlDan No, he has an NSString that he's converting to an NSInteger which means it might be an int (on 32-bit machines) or it might be a long it (on 64-bit machines).
Oh well, I tend to listen to what people say. I'm reading "I have long integer value".
@IlDan yeah I missed the NSString bit the first time reading it, too. =)
Seems to me the poster has a string that contains a number which will overflow a 32 bit value. Therefore NSInteger won't be good enough on a 32 bit machine.
@Stig 2^32 = 4,294,967,296, which is 1,589,209,170 larger than the number given in the question. How would it not fit in a 32-bit int?
|
2

You should have a look at Apple's documentation. NSString has the following method:

- (long long)longLongValue

Which should do what you want.

Comments

1

from this example here, you can see the the conversions both ways:

NSString *str=@"5678901234567890";

long long verylong;  
NSRange range;  
range.length = 15;  
range.location = 0;  

[[NSScanner scannerWithString:[str substringWithRange:range]] 
       scanLongLong:&verylong];

NSLog(@"long long value %lld",verylong);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.