简体   繁体   English

我如何才能使23个十进制整数,然后将其包装在NSNumber中,然后将其包装在Objective-C中

[英]How can I make a 23 decimal integer and then wrap it in NSNumber and then an NSString in objective-c

I want to get 18^18 (23 decimals) into an integer in objective-c, and then into NSNumber and then into NSString. 我想将18 ^ 18(23个十进制)转换为Objective-C中的整数,然后转换为NSNumber,然后转换为NSString。

I can get 15^15 (18 decimals) working as follows 我可以得到15 ^ 15(18位小数)的工作方式如下

15^15 = 437893890380859375 (18 decimal) 15 ^ 15 = 437893890380859375(十进制18)

unsigned long long myFirstInt = pow(self.myBottomNumber, self.myTopNumber);
[self.answerButton setTitle:[[NSNumber numberWithUnsignedLongLong:myFirstInt] stringValue]        forState:UIControlStateNormal];
NSLog(@"ull %lld",myFirstInt);

int64_t myFirstInt = pow(self.myBottomNumber, self.myTopNumber);
[self.answerButton setTitle:[[NSNumber numberWithUnsignedLongLong:myFirstInt] stringValue] forState:UIControlStateNormal];
NSLog(@"int64 %lld",myFirstInt);

-- -

Is there a theorem or algorithm that can pack 20+ decimal numbers into a NSString in objective-c? 是否有一个定理或算法可以将20个以上的十进制数打包到Objective-C中的NSString中?

16^16 = 18446744073709551616 (20 decimal) 16 ^ 16 = 18446744073709551616(十进制20)

17^17 = 827240261886336764177 (21 decimals) 17 ^ 17 = 827240261886336764177(21位小数)

18^18 = 39346408075296537575424 (23 decimal) 18 ^ 18 = 39346408075296537575424(十进制23)

What you want is NSDecimalNumber . 您想要的是NSDecimalNumber It allows you to represent numbers with greater accuracy and range, but with the trade-off that they're slower, take more memory, and can't be manipulated as easily as C-primitive number types. 它允许您以更高的精度和范围来表示数字,但要权衡一下它们较慢,占用更多内存并且无法像C原始数字类型那样容易地进行操作。

Example: 例:

NSDecimalNumber *fifteen = [NSDecimalNumber decimalNumberWithString:@"15"];
NSDecimalNumber *result = [fifteen decimalNumberByRaisingToPower:15];
NSString *string = [result stringValue];

More information can be found here: https://developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html 可以在这里找到更多信息: https : //developer.apple.com/library/mac/documentation/cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM