Objective-C 有没有办法获取一个数字并将其拼写出来?

发布于 2024-11-23 19:51:54 字数 167 浏览 1 评论 0原文

我正在寻找一种方法来获取一个数字(例如 5 或 207),将其拼写出来,并将其序数形式作为字符串返回(五或两百七)。我能找到的只是接受一个数字并返回其序数后缀(st、nd、rd、th)的代码以及英语语法指南中要求您拼写出序数的部分。

我正在寻找一种方法来获取拼写出来的数字及其序数后缀(第五或第二百七)。

I'm looking for a way to take a number (say 5 or 207), spell it out, and returns its ordinal form as a string (five or two hundred seven). All I could find was code that takes a number and returns its ordinal suffix (st, nd, rd, th) and the parts of English grammar guides that say you should spell out ordinals.

I'm looking for a way to get a spelled out number with its ordinal suffix (fifth or two hundred seventh).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

許願樹丅啲祈禱 2024-11-30 19:51:54

更新:我今天遇到这个答案,建议使用 t他的库遵循 MIT 许可证,还支持其他几种语言。希望这对某人有帮助。

旧答案:
我编写了一个脚本,可以做到这一点,但只有英文:

- (NSString*)getSpelledOutNumber:(NSInteger)num
{
    NSNumber *yourNumber = [NSNumber numberWithInt:(int)num];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en"]];
    return [formatter stringFromNumber:yourNumber];
}

- (NSString*)removeLastCharOfString:(NSString*)aString
{
    return [aString substringToIndex:[aString length]-1];
}

- (NSString*)getSpelledOutOrdinalNumber:(NSInteger)num
{    
    NSString *spelledOutNumber = [self getSpelledOutNumber:num];

    // replace all '-'
    spelledOutNumber = [spelledOutNumber stringByReplacingOccurrencesOfString:@"-"
                                                                   withString:@" "];

    NSArray *numberParts = [spelledOutNumber componentsSeparatedByString:@" "];

    NSMutableString *output = [NSMutableString string];

    NSUInteger numberOfParts = [numberParts count];
    for (int i=0; i<numberOfParts; i++) {
        NSString *numberPart = [numberParts objectAtIndex:i];

        if ([numberPart isEqualToString:@"one"])
            [output appendString:@"first"];
        else if([numberPart isEqualToString:@"two"])
            [output appendString:@"second"];
        else if([numberPart isEqualToString:@"three"])
            [output appendString:@"third"];
        else if([numberPart isEqualToString:@"five"])
            [output appendString:@"fifth"];
        else {
            NSUInteger characterCount = [numberPart length];
            unichar lastChar = [numberPart characterAtIndex:characterCount-1];
            if (lastChar == 'y')
            {
                // check if it is the last word
                if (numberOfParts-1 == i)
                { // it is
                    [output appendString:[NSString stringWithFormat:@"%@ieth ", [self removeLastCharOfString:numberPart]]];
                }
                else
                { // it isn't
                    [output appendString:[NSString stringWithFormat:@"%@-", numberPart]];
                }
            }
            else if (lastChar == 't' || lastChar == 'e')
            {
                [output appendString:[NSString stringWithFormat:@"%@th-", [self removeLastCharOfString:numberPart]]];
            }
            else
            {
                [output appendString:[NSString stringWithFormat:@"%@th ", numberPart]];
            }
        }
    }

    // eventually remove last char
    unichar lastChar = [output characterAtIndex:[output length]-1];
    if (lastChar == '-' || lastChar == ' ')
        return [self removeLastCharOfString:output];
    else
        return output;
}

用法非常简单:

NSString *ordinalNumber = [self getSpelledOutOrdinalNumber:42];

数字是“四十秒”。
我希望这对你有帮助。

Update: I came across this answer today, suggesting the use of the use of this library under the MIT-License which also supports a few other languages. Hope this helps someone.

Old Answer:
I coded a script which can this, but only in english:

- (NSString*)getSpelledOutNumber:(NSInteger)num
{
    NSNumber *yourNumber = [NSNumber numberWithInt:(int)num];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en"]];
    return [formatter stringFromNumber:yourNumber];
}

- (NSString*)removeLastCharOfString:(NSString*)aString
{
    return [aString substringToIndex:[aString length]-1];
}

- (NSString*)getSpelledOutOrdinalNumber:(NSInteger)num
{    
    NSString *spelledOutNumber = [self getSpelledOutNumber:num];

    // replace all '-'
    spelledOutNumber = [spelledOutNumber stringByReplacingOccurrencesOfString:@"-"
                                                                   withString:@" "];

    NSArray *numberParts = [spelledOutNumber componentsSeparatedByString:@" "];

    NSMutableString *output = [NSMutableString string];

    NSUInteger numberOfParts = [numberParts count];
    for (int i=0; i<numberOfParts; i++) {
        NSString *numberPart = [numberParts objectAtIndex:i];

        if ([numberPart isEqualToString:@"one"])
            [output appendString:@"first"];
        else if([numberPart isEqualToString:@"two"])
            [output appendString:@"second"];
        else if([numberPart isEqualToString:@"three"])
            [output appendString:@"third"];
        else if([numberPart isEqualToString:@"five"])
            [output appendString:@"fifth"];
        else {
            NSUInteger characterCount = [numberPart length];
            unichar lastChar = [numberPart characterAtIndex:characterCount-1];
            if (lastChar == 'y')
            {
                // check if it is the last word
                if (numberOfParts-1 == i)
                { // it is
                    [output appendString:[NSString stringWithFormat:@"%@ieth ", [self removeLastCharOfString:numberPart]]];
                }
                else
                { // it isn't
                    [output appendString:[NSString stringWithFormat:@"%@-", numberPart]];
                }
            }
            else if (lastChar == 't' || lastChar == 'e')
            {
                [output appendString:[NSString stringWithFormat:@"%@th-", [self removeLastCharOfString:numberPart]]];
            }
            else
            {
                [output appendString:[NSString stringWithFormat:@"%@th ", numberPart]];
            }
        }
    }

    // eventually remove last char
    unichar lastChar = [output characterAtIndex:[output length]-1];
    if (lastChar == '-' || lastChar == ' ')
        return [self removeLastCharOfString:output];
    else
        return output;
}

The usage is pretty simple:

NSString *ordinalNumber = [self getSpelledOutOrdinalNumber:42];

The number would be 'forty-second'.
I hope that helps you.

南风几经秋 2024-11-30 19:51:54

这应该被接受为正确答案。 NSNumberFormatter 将完成这项工作,这是一种标准方法,而不是一些不稳定的解决方法。

这是一个例子:

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSString* stringFromNumber = [numberFormatter stringFromNumber:your_number_goes_here];
NSLog( @"%@", stringFromNumber );

它将输出
“一”代表 1
“二”代表 2
ETC。
此外,它也非常适合非英语语言环境。
例如,如果将数字格式化程序区域设置更改为德语:

numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"DE"];

上面的代码将打印:
'eins' 为 1
'zwei' 代表 2,依此类推。

This should have been accepted as the correct answer. NSNumberFormatter will do the job, and it's a standard approach, not some shaky workaround.

Here is an example:

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSString* stringFromNumber = [numberFormatter stringFromNumber:your_number_goes_here];
NSLog( @"%@", stringFromNumber );

It will output
'one' for 1
'two' for 2
etc.
Besides, it works perfectly for nonEnglish locales as well.
For example, if you change the number formatter locale to German:

numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"DE"];

the code above will print:
'eins' for 1
'zwei' for 2 and so on.

小兔几 2024-11-30 19:51:54

我曾经写过一个名为 vpi2english 的工具。它是我的 vpi 工具箱的一部分,用 matlab 编写。但代码确实非常简单,如果您选择,可以转换为另一种语言。它本质上需要一个(十进制)数字字符串,一次将其分解为三个数字的片段,并将它们分别写成单词。

>> vpi2english(vpi('2331546567543686356564321'))
ans =
   two septillion, three hundred thirty one sextillion,
five hundred forty six quintillion, five hundred sixty seven quadrillion,
five hundred forty three trillion, six hundred eighty six billion,
three hundred fifty six million, five hundred sixty four thousand,
three hundred twenty one 

目前,它适用于小于 1e306 的 1 的数字,这是我在网上可以找到的此类数字名称的最大数字。

>> vpi2english(999999*vpi(10)^300)
ans =
nine hundred ninety nine centillion, nine hundred ninety nine novemnonagintillion

I once wrote a tool called vpi2english. It is part of my vpi toolbox, written in matlab. But the code is pretty simple really, and could be converted to another language if you choose. It essentially takes a (decimal) digit string, breaks it down into pieces of three digits at a time, and writes them each in words.

>> vpi2english(vpi('2331546567543686356564321'))
ans =
   two septillion, three hundred thirty one sextillion,
five hundred forty six quintillion, five hundred sixty seven quadrillion,
five hundred forty three trillion, six hundred eighty six billion,
three hundred fifty six million, five hundred sixty four thousand,
three hundred twenty one 

It currently works on numbers as large as 1 less than 1e306, which is as large as I could find names for such numbers online.

>> vpi2english(999999*vpi(10)^300)
ans =
nine hundred ninety nine centillion, nine hundred ninety nine novemnonagintillion
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文