在 Objective-C 中有没有办法取一个数字并拼写出来? [英] Is there a way in Objective-C to take a number and spell it out?

查看:43
本文介绍了在 Objective-C 中有没有办法取一个数字并拼写出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取一个数字(例如 5 或 207),将其拼写出来,然后将其序数形式作为字符串(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).

推荐答案

更新:我遇到了这个今天回答,建议使用MIT-License下的t他的库它还支持其他几种语言.希望这对某人有所帮助.

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;
}

用法很简单:

NSString *ordinalNumber = [self getSpelledOutOrdinalNumber:42];

数字将是四十秒".希望能帮到你.

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

这篇关于在 Objective-C 中有没有办法取一个数字并拼写出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆