I've below numbers which are stored as NSCFString into SQLite
These numbers converts perfectly
475602307163925662
1529228456639250520
I am converting these like , (NSNumber *) [myDictionary valueForKey:@"stringData"];
and its working perfectly
These numbers couldn't converts perfectly
14154269406789154303
13207084142614401684
12870871772958895646
I want to convert these NSCFString into NSNumber. I try using below code and google it but dont get exact solution as I want. Any help would be appriciated.
NSNumberFormatter *f=[[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *myNumber=[f numberFromString:[myDictionary valueForKey:@"stringData"]];
[f release];
but it converts my number something like 9.45214212452152
I tried some other way as well but its also not working well.
NSNumber *myNumber=[NSNumber numberWithLong:[myDictionary valueForKey:@"stringData"]];
Casting an object as another object (aka (NSNumber *)
) does not "convert" it. It just promises the compiler that you're working with an object of that class which you aren't .
The reason why some numbers are totally failing is because they're too large to fit in the standard int/float/long sizes. Instead of NSNumber
, use NSDecimalNumber
- a subclass of NSNumber
(so you can still use all of the NSNumber
methods as well). It's specifically designed to handle large numbers:
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:<your string object>];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.