首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 數字轉中文大寫/指示性字符:陣列映射法

     編輯:程式設計綜合問答

需求

資料庫讀取的數值為 1, 2, 3...,需在 Label 顯示為 一, 二, 三... 或 one, two, three...。

核心思路:建立映射陣列/字典,索引即數值

中文數字(含大寫金額)

// 簡體/繁體通用
NSArray *chineseDigits = @[@"零", @"一", @"二", @"三", @"四", @"五", @"六", @"七", @"八", @"九"];
NSArray *chineseUnits  = @[@"", @"十", @"百", @"千", @"萬", @"億"];

// 簡易轉換(0-9999)
- (NSString *)numberToChinese:(NSInteger)num {
    if (num == 0) return chineseDigits[0];
    NSMutableString *result = [NSMutableString string];
    NSInteger unitIndex = 0;
    while (num > 0) {
        NSInteger digit = num % 10;
        if (digit != 0) {
            [result insertString:chineseDigits[digit] atIndex:0];
            [result insertString:chineseUnits[unitIndex] atIndex:0];
        } else if (result.length > 0 && ![[result substringToIndex:1] isEqualToString:chineseDigits[0]]) {
            [result insertString:chineseDigits[0] atIndex:0]; // 中間補零
        }
        num /= 10;
        unitIndex++;
    }
    // 處理「一十」→「十」
    if ([result hasPrefix:@"一十"]) [result deleteCharactersInRange:NSMakeRange(0, 1)];
    return result;
}

英文單詞映射(0-99)

NSArray *englishWords = @[
    @"zero", @"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine",
    @"ten", @"eleven", @"twelve", @"thirteen", @"fourteen", @"fifteen", @"sixteen", @"seventeen", @"eighteen", @"nineteen",
    @"twenty", @"thirty", @"forty", @"fifty", @"sixty", @"seventy", @"eighty", @"ninety"
];

- (NSString *)numberToEnglish:(NSInteger)num {
    if (num < 20) return englishWords[num];
    if (num < 100) {
        NSInteger tens = num / 10;
        NSInteger ones = num % 10;
        NSString *tensWord = englishWords[18 + tens]; // twenty 起始索引 18
        return ones == 0 ? tensWord : [NSString stringWithFormat:@"%@-%@", tensWord, englishWords[ones]];
    }
    return @""; // 超出範圍自行擴充
}

Swift 版字典映射(更簡潔)

let chineseMap: [Int: String] = [
    0: "零", 1: "一", 2: "二", 3: "三", 4: "四",
    5: "五", 6: "六", 7: "七", 8: "八", 9: "九"
]

func toChinese(_ num: Int) -> String {
    guard num != 0 else { return chineseMap[0]! }
    var n = num, result = "", unit = 0
    let units = ["", "十", "百", "千", "萬", "億"]
    while n > 0 {
        let d = n % 10
        if d != 0 {
            result = "\(chineseMap[d]!)\(units[unit])\(result)"
        } else if !result.isEmpty && !result.hasPrefix("零") {
            result = "零\(result)"
        }
        n /= 10; unit += 1
    }
    return result.replacingOccurrences(of: "^一十", with: "十", options: .regularExpression)
}

使用範例

NSInteger dbValue = 25;
label.text = [self numberToChinese:dbValue]; // "二十五"
label.text = [self numberToEnglish:dbValue]; // "twenty-five"

擴充建議

  • 支援負數、小數:先處理符號與小數點,分別轉換整數部與小數部。
  • 大寫金額(壹貳參...):建立對應映射表,並依《人民幣大寫》規則插入「整」「元」「角」「分」。
  • 本地化:將映射表放入 .strings 檔案,支援多語言切換。

「查表法」是數字轉文字最高效、最易維護的模式,避免複雜遞迴或條件判斷。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved