-1

I am a newbie in programming and i start with Objective C as my first language. I am messing around with some books and tutorials, at last programing a calculator... Everything fine and i am getting into (programming makes really fun)

Now i am asking myself how I could translate arabic numbers to chinese numbers (e.g. arabic 4 is in chinese 四 and 8 is 八 which means 四 + 四 = 八 The chinese number system is kind of different than arabic they have signs for 100, 1000, 10000 and ja kind of twisted, which screws up my brain ... anyway do anybody have some advice, hints, tips or solutions how i can tell the computer how to work with this numbers, or even how to calculate with them?

I think everything is possible so i wont ask "If its even possible?"

5
  • 1
    What have you tried?
    – Sebastian
    Feb 25, 2013 at 21:26
  • MSB, how would you do it with pencil and paper? If you haven't figured out how to do it with pencil and paper, you're not ready to do it with a programming language.
    – Joe
    Feb 25, 2013 at 21:46
  • @Sebastian I just finished my calc tut, changing the NSString stringWithFormat to e.g: "@"%@四", ... now reading about"enum"
    – MSB
    Feb 25, 2013 at 21:50
  • @Joe: Iam learning chinese since a couple of years and i know how i would to this with "pencil and paper" but honestly is pain in the ass to calc with chinese numbers but chinese elementary kids learning this in school before they switching to arabic numbers ... anyway Joe do u have another suggestion? And yes i am not ready to do this in a programming language but without problems how should i learning such an abstract thing?
    – MSB
    Feb 25, 2013 at 21:54
  • Computers don't do math using symbols like "4" or "四". They convert symbols like those into their own internal number representation. Are you trying to convert Chinese symbols into European symbols, or are you trying to have the computer read in Chinese symbols and convert them to its internal number representation?
    – Joe
    Feb 25, 2013 at 22:04

3 Answers 3

2

Considering the Chinese numerical system (Mandarin) as described by wikipedia http://en.wikipedia.org/wiki/Chinese_numerals, where for instance:

  • 45 is interpreted as [4] [10] [5] and written 四十五
  • 114 is interpreted as [1] [100] [1] [10] [4] and written 一百一十四

So the trick is to decompose a number as powers of 10:

x = c(k)*10^k + ... + c(1)*10 + c(0)

where k is the largest power of 10 that divides x such that the quotient is at least 1. In the 2nd example above, 114 = 1*10^2 + 1*10 + 4.

This x = c(k)*10^k + ... + c(1)*10 + c(0) becomes [c(k)][10^k]...[c(1)][10][c(0)]. In the 2nd example again, 114 = [1] [100] [1] [10] [4].

Then map each number within bracket to the corresponding sinogram:

0 = 〇

1 = 一

2 = 二

3 = 三

4 = 四

5 = 五

6 = 六

7 = 七

8 = 八

9 = 九

10 = 十

100 = 百

1000 = 千

10000 = 万

As long as you keep track of the [c(k)][10^k]...[c(1)][10][c(0)] form, it's easy to convert to an integer that the computer can handle or to the corresponding Chinese numeral. So it's this [c(k)][10^k]...[c(1)][10][c(0)] form that I'd store in an integer array of size k+2.

2
  • That doesn't even begin to consider all exceptions
    – user6269864
    Sep 18, 2017 at 7:12
  • No, this answer is a starting point. A good reference to an enumeration of all the exceptions would be useful, though. Sep 29, 2017 at 20:30
1

I'm not familiar with Objective-C, thus I can't help you with a solution for iOS. Nonetheless, following is the Java code for Android... I assumed it might help you, as well as it helped me.

double text2double(String text) {


    String[] units = new String[] { "〇", "一", "二", "三", "四",
            "五", "六", "七", "八", "九"};

    String[] scales = new String[] { "十", "百", "千", "万",
            "亿" };

    HashMap<String, ScaleIncrementPair> numWord = new HashMap<String, ScaleIncrementPair>();

    for (int i = 0; i < units.length; i++) {
        numWord.put(units[i], new ScaleIncrementPair(1, i));
    }   

    numWord.put("零", new ScaleIncrementPair(1, 0));
    numWord.put("两", new ScaleIncrementPair(1, 2));

    for (int i = 0; i < scales.length; i++) {
        numWord.put(scales[i], new ScaleIncrementPair(Math.pow(10, (i + 1)), 0));
    }

    double current = 0;
    double result = 0;

    for (char character : text.toCharArray()) {

        ScaleIncrementPair scaleIncrement = numWord.get(String.valueOf(character));
        current = current * scaleIncrement.scale + scaleIncrement.increment;
        if (scaleIncrement.scale > 10) {
            result += current;
            current = 0;
        }
    }

    return result + current;
}

class ScaleIncrementPair {
    public double scale;
    public int increment;

    public ScaleIncrementPair(double s, int i) {
        scale = s;
        increment = i;
    }
}
1

You can make use of NSNumberFormatter.

Like below code, firstly get NSNumber from chinese characters, then combine them.

func getNumber(fromText text: String) -> NSNumber? {
    let locale = Locale(identifier: "zh_Hans_CN")
    let numberFormatter = NumberFormatter()
    numberFormatter.locale = locale
    numberFormatter.numberStyle = .spellOut
    guard let number = numberFormatter.number(from: text) else { return nil }
    print(number)
    return number
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.