如何在 Objective C 中显示波斯语文本中的数字,如 125 显示 "one hundred - twenty five"?

How to show the numbers in Persian text in Objective C like 125 show "one hundred - twenty five"?

NSString *numberWord;
NSNumber *numberValue = [NSNumber numberWithInt:125];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
numberWord = [numberFormatter stringFromNumber:numberValue];

//我要显示“一百二十五”

您需要将数字格式化程序的区域设置设置为波斯语区域设置。

NSNumber *numberValue = @(125);
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fa"]];
NSString *numberWord = [numberFormatter stringFromNumber:numberValue];

这将输出:

صد و بیست و پنج

或者创建一个方法:-

//Call this methods from you class to get the string by passing number and Locale string
-(NSString *)getNumberString:(NSNumber *)num withLocale:(NSString *)locale{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
    [numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:locale]];
    return [numberFormatter stringFromNumber:num];
}

通过传递数字和所需的语言环境来获取字符串

[self GetNumberString:@(1007) withLocale:@"fa"];//Use different Locale (en,es,sn)

Swift 4

func getTextOfNumber(num: Int, identifier: String) -> String {
    let number: NSNumber = NSNumber(value: num)

    let formatter = NumberFormatter()
    formatter.numberStyle = .spellOut
    formatter.locale = Locale(identifier: identifier)

    return formatter.string(from: number) ?? String(num)
}

let text = getTextOfNumber(num: 230, identifier: "fa")
print(text)

输出:

دویست و سی

或用作扩展

extension Int {
func getText(identifier: String) -> String {
        let number: NSNumber = NSNumber(value: self)
        let formatter = NumberFormatter()

        formatter.numberStyle = .spellOut
        formatter.locale = Locale(identifier: identifier)

        return formatter.string(from: number) ?? String(self)
    }
}

print(230.getText(identifier: "fa"))

دویست و سی