我对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