Goal C strange decimal and long conversion

NSLog(@"%llu\n\n", ULONG_LONG_MAX); NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:@"154550038129946620"]; NSLog(@"%@", decimal); NSLog(@"%llu\n\n", [decimal unsignedLongLongValue]); decimal = [NSDecimalNumber decimalNumberWithString:@"154550038129946628"]; NSLog(@"%@", decimal); NSLog(@"%llu", [decimal unsignedLongLongValue]); 

Both values ​​are less than ULONG_LONG_MAX. But, what we see in the output:

 2012-01-05 17:41:55.879 test[1276:207] 18446744073709551615 2012-01-05 17:41:55.969 test[1276:207] 154550038129946620 2012-01-05 17:41:56.095 test[1276:207] 154550038129946624 2012-01-05 17:41:56.096 test[1276:207] 154550038129946628 2012-01-05 17:41:56.096 test[1276:207] 154550038129946624 

What am I doing wrong? I do not know how to explain this behavior.

+6
source share
1 answer

NSDecimalNumber missing the unsignedLongLongValue method, so it inherits the default implementation from NSNumber . The only ways to get the value of NSDecimalNumber are to get an NSDecimal or double structure. A conversion error is introduced during the conversion of NSDecimalNumber to double .

+5
source

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


All Articles