private Number parseNumber(int max, String string, ParsePosition position) { int length = string.length(); int index = position.getIndex(); if (max > 0 && max < length - index) { length = index + max; } while (index < length && (string.charAt(index) == ' ' || string.charAt(index) == '\t')) { ++index; } if (max == 0) { position.setIndex(index); Number n = numberFormat.parse(string, position); // In RTL locales, NumberFormat might have parsed "2012-" in an ISO date as the // negative number -2012. // Ideally, we wouldn't have this broken API that exposes a NumberFormat and expects // us to use it. The next best thing would be a way to ask the NumberFormat to parse // positive numbers only, but icu4c supports negative (BCE) years. The best we can do // is try to recognize when icu4c has done this, and undo it. if (n != null && n.longValue() < 0) { if (numberFormat instanceof DecimalFormat) { DecimalFormat df = (DecimalFormat) numberFormat; char lastChar = string.charAt(position.getIndex() - 1); char minusSign = df.getDecimalFormatSymbols().getMinusSign(); if (lastChar == minusSign) { n = Long.valueOf(-n.longValue()); // Make the value positive. position.setIndex(position.getIndex() - 1); // Spit out the negative sign. } } } return n; } int result = 0; int digit; while (index < length && (digit = Character.digit(string.charAt(index), 10)) != -1) { result = result * 10 + digit; ++index; } if (index == position.getIndex()) { position.setErrorIndex(index); return null; } position.setIndex(index); return Integer.valueOf(result); }