objective-c - 如何将整数转换为 objective-c 中的相应单词?

标签 objective-c ios

在 iOS 上的 Objective-C 中有没有办法将整数拼写为文本?

例如,如果我有

NSInteger someNumber = 11242043;

我想知道一些函数,以便返回类似于“11242043”的字符串。

最佳答案

Apple 为许多数据类型内置了许多方便的格式化功能。它们被称为“格式化程序”,可以将对象与字符串表示形式相互转换。

对于您的情况,您将使用 NSNumberFormatter,但如果您有整数,则需要先将其转换为 NSNumber。请参见下面的示例。

NSInteger anInt = 11242043;
NSString *wordNumber;

//convert to words
NSNumber *numberValue = [NSNumber numberWithInt:anInt]; //needs to be NSNumber!
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
wordNumber = [numberFormatter stringFromNumber:numberValue];
NSLog(@"Answer: %@", wordNumber);
// Answer: eleven million two hundred forty-two thousand forty-three

如果您想了解有关格式化程序的更多信息: https://developer.apple.com/library/content/documentation/General/Conceptual/Devpedia-CocoaApp/Formatter.html

关于objective-c - 如何将整数转换为 objective-c 中的相应单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250433/

相关文章:

ios - 如何在 Swift 中使用 UISlider 更改 UIImageView 中的图像

ios - Xcode 7 无法从框架中找到头文件

ios - UITableView 部分标题的自动布局问题

ios - 启动时黑屏,但按主页按钮再次进入应用程序时正常

ios - 核心数据与 SQLite

ios - 静态 NSManagedObject 将保留在内存中

java - 将 C++ 与 Objective-C 和 Java 混合使用

objective-c - 在 Cocoa/OSX 中覆盖鼠标图像

iphone - 核心音频,Goertzel 算法不起作用

objective-c - iOS 中弃用的常量