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]);
两个值都小于ULONG_LONG_MAX。但是,我们在输出中看到的是:
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
我做错了什么?我不知道如何解释这种行为。
答案 0 :(得分:5)
NSDecimalNumber
缺少unsignedLongLongValue
方法,因此它从NSNumber
继承了默认实现。获得NSDecimalNumber
价值的唯一方法是获得NSDecimal
结构或double
。转换错误是在将NSDecimalNumber
转换为double
。