0

I really can't figure out how to convert large NSNumber to NSString (4h googling not solved my problem and tried all suggested solutions - stringValue, NSNumberFormatter, NSDecimalNumber).

Currently I converting in this way:

[NSString stringWithFormat:@"%llu", [largeNumber unsignedLongLongValue]];

All works fine with small numbers but with larger I get wrong strings :( Example: Input number is 10202713632509569, but returned string is 10202713632509568. How to convert NSNumber to NSString in proper way?

I get NSNumber from JSON response with AFNetworking framework.

My JSON:

{
    "author": {
        "name": "Open Graph Test User",
        "social_type": "facebook",
        "id": 10202713632509569
    }
}

My code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager new];
manager.responseSerializer             = [AFJSONResponseSerializer new];

[manager GET:@"/api/author"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSNumber *largeNumber = responseObject[@"author"][@"social_id"];
         // ...
         NSString *callPath = [NSString stringWithFormat:@"/profile/%@/%@", _userSocialType, [NSString stringWithFormat:@"%llu", [largeNumber unsignedLongLongValue]]];
         // ...
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // ...
     }
  }];
urfin
  • 33
  • 1
  • 6
  • When you try it with 'largeNumber.stringValue', what are you getting? – wibosco Jul 27 '15 at 17:30
  • I am getting this: 1.020271363250957e+16 – urfin Jul 27 '15 at 17:35
  • 1
    What is the purpose of storing this large number as a number and not directly as a string? Can you break the number into an Upper and Lower subset where they both are a power of 10 (only necessary for calculations). – Matt Jul 27 '15 at 17:38
  • I get NSNumber from JSON response and need string representation of this number for URL creation. – urfin Jul 27 '15 at 17:42
  • @urfin Perhaps you run into the problem where the JSON parser is storing your number as floating-point rather than as an integer. – The Paramagnetic Croissant Jul 27 '15 at 17:46
  • Have you tried `NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // adjust this NSString *formattedOutput = [formatter stringFromNumber:yourDecimalNumber];` to get rid of scientific notation? – Cole Jul 27 '15 at 17:46
  • 1
    Isn't JSON already text, and therefore a string? – Matt Jul 27 '15 at 17:47
  • @TheParamagneticCroissant I am using AFJSONRequestSerializer from AFNetworking – urfin Jul 27 '15 at 17:53
  • @Cole Yes, I tried this with NSNumberFormatterNoStyle (result: 10202713632509600 - incorrect) and NSNumberFormatterDecimalStyle (result: 10,202,713,632,509,600 - incorrect) – urfin Jul 27 '15 at 17:57
  • @Matt No, in JSON I have raw number – urfin Jul 27 '15 at 17:58
  • Show us how you create the `largeNumber`! And no, technically you have the value as string, since the json is basically a string. – luk2302 Jul 27 '15 at 17:59
  • @luk2302 yes, he does, but that's not terribly useful since the JSON parser converts it to a number. – The Paramagnetic Croissant Jul 27 '15 at 18:03
  • @luk2302 This is my JSON response: http://pastebin.com/9FShpcHp. I am not creating largeNumber manually - I am using AFNetworking framework for JSON handling – urfin Jul 27 '15 at 18:05
  • @TheParamagneticCroissant just saying "technichally"... – luk2302 Jul 27 '15 at 18:05
  • Show us your **code**. You are referencing `largeNumber`, where does it come from? is it a simplification, don't over-simplificate things because you might remove the useful information. – luk2302 Jul 27 '15 at 18:06
  • @luk2302 This is my code - http://pastebin.com/JemWXXr8 – urfin Jul 27 '15 at 18:17
  • Your NSNumber is in `double` format, and only contains 16 decimal digits of precision. You must use NSDecimalNumber or int64 to get higher precision. – Hot Licks Jul 27 '15 at 18:25
  • @HotLicks but the problem is he doesn't get to control that. It's the JSON parser that is outputting a double. – The Paramagnetic Croissant Jul 27 '15 at 18:28
  • 1
    @TheParamagneticCroissant - He should check whether the parser is indeed returning a NSDecimalNumber, and, if so, use an alternate method to extract it's value. (I hassled with this scenario quite a bit, and NSJSONSerialization does some odd but usually unwindable conversions to maintain significance for numbers in this range.) – Hot Licks Jul 27 '15 at 18:35

0 Answers0