目标C奇数十进制到长期转换

时间:2012-01-05 13:44:17

标签: objective-c decimal long-integer

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

我做错了什么?我不知道如何解释这种行为。

1 个答案:

答案 0 :(得分:5)

NSDecimalNumber缺少unsignedLongLongValue方法,因此它从NSNumber继承了默认实现。获得NSDecimalNumber价值的唯一方法是获得NSDecimal结构或double。转换错误是在将NSDecimalNumber转换为double

的过程中引入的