如何在Objective C中显示波斯文本中的数字,如125表示“一百二十五”?

时间:2018-04-07 05:22:45

标签: objective-c persian

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

//我要展示“صدوبیستوپنج”

3 个答案:

答案 0 :(得分:2)

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

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

这将输出:

  

صدوبیستوپنج

答案 1 :(得分:0)

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"))
  

دویستوسی

答案 2 :(得分:-1)

或创建方法: -

//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)