Desperately seeking strtod()




This is probably simple, but I must be looking in
the wrong places ... I'm trying to do what the strtod()
function does for me in C: Find the longest prefix of a
string that looks like a number, produce its numeric
value, and tell me where the unconverted suffix starts.

This sounds like a job for DecimalFormat.parse(), but
I can't get it to accept all the things I'd like it to
consider as numbers. Constructed from the pattern "0E0"
or "#E0" (or "0e0" or "#e0"), a DecimalFormat will happily
parse "1" and "1.2" and ".2" and "-1.2E3", and "1E-3", but
it rejects "+1" and "1e3" and "1E+3". Some of the rejections
are outright failures ("+1" gives a null result, for example),
while others are incomplete conversions ("1E+3" produces the
value 1 with "E+3" as the unconverted suffix).

Now, Double.valueOf() happily accepts and does the
right thing with all these forms -- but it insists on
converting the entire string, not just a prefix. It'd
be possible to do a sort of binary search to find the
longest prefix that doesn't provoke NumberFormatException,
but my stomach turns at the thought of such vileness.

Please help an old C programmer who's trying to reform:
Where do I find the Java equivalent of strtod()?

--

Eric.Sosman@xxxxxxx

.