FloatingPointParserpublic final class FloatingPointParser extends Object Used to parse a string and return either a single or double precision
floating point number. |
Methods Summary |
---|
private static org.apache.harmony.luni.util.FloatingPointParser$StringExponentPair | initialParse(java.lang.String s, int length)Takes a String and does some initial parsing. Should return a
StringExponentPair containing a String with no leading or trailing white
space and trailing zeroes eliminated. The exponent of the
StringExponentPair will be used to calculate the floating point number by
taking the positive integer the String represents and multiplying by 10
raised to the power of the of the exponent.
boolean negative = false;
char c;
int start, end, decimal;
int e = 0;
start = 0;
if (length == 0)
throw new NumberFormatException(s);
c = s.charAt(length - 1);
if (c == 'D" || c == 'd" || c == 'F" || c == 'f") {
length--;
if (length == 0)
throw new NumberFormatException(s);
}
end = Math.max(s.indexOf('E"), s.indexOf('e"));
if (end > -1) {
if (end + 1 == length)
throw new NumberFormatException(s);
int exponent_offset = end + 1;
if (s.charAt(exponent_offset) == '+") {
if (s.charAt(exponent_offset + 1) == '-") {
throw new NumberFormatException(s);
}
exponent_offset++; // skip the plus sign
}
try {
e = Integer.parseInt(s.substring(exponent_offset,
length));
} catch (NumberFormatException ex) {
// ex contains the exponent substring
// only so throw a new exception with
// the correct string
throw new NumberFormatException(s);
}
} else {
end = length;
}
if (length == 0)
throw new NumberFormatException(s);
c = s.charAt(start);
if (c == '-") {
++start;
--length;
negative = true;
} else if (c == '+") {
++start;
--length;
}
if (length == 0)
throw new NumberFormatException(s);
decimal = s.indexOf('.");
if (decimal > -1) {
e -= end - decimal - 1;
s = s.substring(start, decimal) + s.substring(decimal + 1, end);
} else {
s = s.substring(start, end);
}
if ((length = s.length()) == 0)
throw new NumberFormatException();
end = length;
while (end > 1 && s.charAt(end - 1) == '0")
--end;
start = 0;
while (start < end - 1 && s.charAt(start) == '0")
start++;
if (end != length || start != 0) {
e += length - end;
s = s.substring(start, end);
}
return new StringExponentPair(s, e, negative);
| private static native double | parseDblImpl(java.lang.String s, int e)Takes a String and an integer exponent. The String should hold a positive
integer value (or zero). The exponent will be used to calculate the
floating point number by taking the positive integer the String
represents and multiplying by 10 raised to the power of the of the
exponent. Returns the closest double value to the real number
| private static double | parseDblName(java.lang.String namedDouble, int length)
// Valid strings are only +Nan, NaN, -Nan, +Infinity, Infinity,
// -Infinity.
if ((length != 3) && (length != 4) && (length != 8) && (length != 9)) {
throw new NumberFormatException();
}
boolean negative = false;
int cmpstart = 0;
switch (namedDouble.charAt(0)) {
case '-":
negative = true; // fall through
case '+":
cmpstart = 1;
default:
}
if (namedDouble.regionMatches(false, cmpstart, "Infinity", 0, 8)) {
return negative ? Double.NEGATIVE_INFINITY
: Float.POSITIVE_INFINITY;
}
if (namedDouble.regionMatches(false, cmpstart, "NaN", 0, 3)) {
return Double.NaN;
}
throw new NumberFormatException();
| public static double | parseDouble(java.lang.String s)Returns the closest double value to the real number in the string.
s = s.trim();
int length = s.length();
if (length == 0) {
throw new NumberFormatException(s);
}
// See if this could be a named double
char last = s.charAt(length - 1);
if ((last == 'y") || (last == 'N")) {
return parseDblName(s, length);
}
// See if it could be a hexadecimal representation
if (s.toLowerCase().indexOf("0x") != -1) { //$NON-NLS-1$
return HexStringParser.parseDouble(s);
}
StringExponentPair info = initialParse(s, length);
double result = parseDblImpl(info.s, info.e);
if (info.negative)
result = -result;
return result;
| public static float | parseFloat(java.lang.String s)Returns the closest float value to the real number in the string.
s = s.trim();
int length = s.length();
if (length == 0) {
throw new NumberFormatException(s);
}
// See if this could be a named float
char last = s.charAt(length - 1);
if ((last == 'y") || (last == 'N")) {
return parseFltName(s, length);
}
// See if it could be a hexadecimal representation
if (s.toLowerCase().indexOf("0x") != -1) { //$NON-NLS-1$
return HexStringParser.parseFloat(s);
}
StringExponentPair info = initialParse(s, length);
float result = parseFltImpl(info.s, info.e);
if (info.negative)
result = -result;
return result;
| private static native float | parseFltImpl(java.lang.String s, int e)Takes a String and an integer exponent. The String should hold a positive
integer value (or zero). The exponent will be used to calculate the
floating point number by taking the positive integer the String
represents and multiplying by 10 raised to the power of the of the
exponent. Returns the closest float value to the real number
| private static float | parseFltName(java.lang.String namedFloat, int length)
// Valid strings are only +Nan, NaN, -Nan, +Infinity, Infinity,
// -Infinity.
if ((length != 3) && (length != 4) && (length != 8) && (length != 9)) {
throw new NumberFormatException();
}
boolean negative = false;
int cmpstart = 0;
switch (namedFloat.charAt(0)) {
case '-":
negative = true; // fall through
case '+":
cmpstart = 1;
default:
}
if (namedFloat.regionMatches(false, cmpstart, "Infinity", 0, 8)) {
return negative ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
}
if (namedFloat.regionMatches(false, cmpstart, "NaN", 0, 3)) {
return Float.NaN;
}
throw new NumberFormatException();
|
|