Strange Java bug

B

Brad Foster

The following does not throw a numberformat exception like it should

double dbl = Double.parseDouble ("1D");

but this does



double dbl = Double.parseDouble ("1A");





whats going on here???
 
B

Brad Foster

yes i guessed that ... works the same way for float...but it is really
strange!! Im using this method to check if my string is numeric or not

Looks like it will fail for all string ending with f or d!!!
 
B

Bjoern

Brad said:
yes i guessed that ... works the same way for float...but it is really
strange!! Im using this method to check if my string is numeric or not

Looks like it will fail for all string ending with f or d!!!

I guess the Java way of doing it would be to use the NumberFormat
classes from java.text, because of the localization scheme.

[...]

--


Let's not weep for their evil deeds,
but for their lack of imagination
(Nick Cave)
 
S

Steven Coco

Roedy said:
Probably someone quietly allowed the trailing letter literals in the
Java language to be allowed in literals discovered in strings.

Unfortunately; the doc comments will tell you that parseDouble returns a
value "as performed by the valueOf method", whose behavior is "defined
in §3.10.2 of the Java Language Specification." This should ultimately
allow 'f', 'F', 'd', or 'D' to follow the digits and convert
successfully; however: note that even if you pass in a trailing 'f' or
'F', the value will be converted directly to a double (not first to a
float). See the doc comments.

--

..Steven Coco.
.........................................................................
When you're not sure:
"Confess your heart" says the Lord, "and you'll be freed."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top