Trying to save a long time in NSNumber from String

I am trying to store a long long number (obtained as a string) such as "80182916772147201" in NSNumber.

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterBehaviorDefault];

[item setObject:[f numberFromString:@"80182916772147201"] forKey:@"theID"];
[f release];

When I NSLog this, if the line was "80182916772147201", I get:

NSLog(@"%lld", [[item objectForKey:@"theID"] longLongValue]); 

Returns: '80182916772147200' - Pay attention to the rounded end digit.

What am I doing wrong?

+3
source share
2 answers

The problem is that NSNumberFormatterI decided to present this number as a floating point number. To make it use only integers:

[f setAllowsFloats:NO];
+3
source

Can you try this?

NSString *numStr = [NSString stringWithFormat:@"%llu", [myNum unsignedLongLongValue]];

, numStr, "" unsigned long long. , UTF8String , [[numStr dataUsingEncoding: NSUTF8StringEncoding] bytes], , , - 32 . , - .

, - unsignedLongLongValue NSString, , () SO. , rklIntValue, unsignedLongLongValue.

+2
source

All Articles