我對Objective-C不熟悉,因此我無法幫助您使用iOS的解決方案。 儘管如此,以下是適用於Android的Java代碼... 我認爲它可以幫助您,以及它對我有幫助。
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;
}
}
[你有什麼試過的?](http://whathaveyoutried.com) – Sebastian 2013-02-25 21:26:06
MSB,你會用鉛筆和紙做什麼?如果你還沒有想出如何用鉛筆和紙做這件事,那麼你還沒有準備好用編程語言去做。 – Joe 2013-02-25 21:46:17
@塞巴斯蒂安我剛完成了我的calc tut,將NSString stringWithFormat改爲例如:「@」%@四「,...現在閱讀關於」枚舉「 – MSB 2013-02-25 21:50:28