NSNumber double with many decimals, rounded / truncated

I have doublein NSNumber.

double myDouble = 1363395572.6129999;

NSNumber *doubleNumber = @(myDouble); 
// using [NSNumber numberWithDouble:myDouble] leads to the same result

Here it becomes problematic.

doubleNumber.doubleValueseems to return the correct and complete value (1363395572.6129999)

However, looking at doubleNumberthe debugger or doing doubleNumber.description, they give me (1363395572.613).

I would understand, maybe it was just some display formatting, but when I insert this object into the JSON payload, the inserted rounded value is inserted instead of the actual number.

The way I do it looks something like this:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:(Dictionary containing NSNumber)
                                                           options:0 error:nil];

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Looking at the line at this point, I see a truncated number with 3 decimal places, although the one NSNumberI inserted was 7.

My question is: why is this happening and more importantly, how can I stop it?

EDIT with the conclusion:

, , , , NSNumber double , . , , JSON.

, , ( , ) API,

, , JSON, JSON NSDecimalNumber, NSNumber. , , , , .

+4
4

, double 16 . 1363395572.612999 17 double , 1363395572.613:

double myDouble = 1363395572.6129999;
double myDouble1 = 1363395572.613;

NSLog(@"%.20f", myDouble);  // 1363395572.61299991607666015625
NSLog(@"%.20f", myDouble1); // 1363395572.61299991607666015625
NSLog(@"%s", myDouble == myDouble1 ? "equal" : "different"); // equal

double 1363395572.613 .

- "1363395572.6129999", double, . NSDecimalNumber ( 38 ):

NSDecimalNumber *doubleNumber = [NSDecimalNumber decimalNumberWithString:@"1363395572.6129999"];
NSDictionary *dict = @{@"key": doubleNumber};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// {"key":1363395572.6129999}

long double NSDecimalNumber:

long double ld1 = 1363395572.6129999L;
long double ld2 = 1363395572.613L;

NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.7Lf", ld1]];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.7Lf", ld2]];

NSDictionary *dict = @{@"key1": num1, @"key2": num2};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// {"key1":1363395572.6129999,"key2":1363395572.613}

. , , JSON, . , NSJSONSerialization , "" JSON:

NSString *jsonString = @"{\"key1\":1363395572.6129999,\"key2\":1363395572.613}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict2 = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
NSNumber *n1 = dict2[@"key1"];
NSNumber *n2 = dict2[@"key2"];

BOOL b = [n1 isEqualTo:n2]; // YES
+9

NSDecimalNumber:

NSDecimalNumber* dc = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.7f", myDouble]];

.


, NSDecimalNumber, . double, .

+3

, , , . , JSON- , .

8- ... , LP64 longs. - , .

+1

:

double myDouble = 1363395572.6129999;
NSString *s = [NSString stringWithFormat:@"%.7f", myDouble];
NSLog(@"%@", s);

:

+1363395572,6129999

, , JSON, .

EDIT: If you need higher accuracy, and not just control over what is included in JSON, it will be long doublesaved. Thus:

long double myDouble = 1363395572.6129999;
NSString *s = [NSString stringWithFormat:@"%.7L", myDouble];
NSLog(@"%@", s);
+1
source

Source: https://habr.com/ru/post/1536155/


All Articles