Upper Case Conversion Required

I wrote the code snip below to do some extra checking on string to
double conversion. I did not expect the need to call toUpperCase.
What was I not anticipating? It just didn't seem logical to me,
given the number of tools out there that create their floating point
numbers with a lower case "e".

Advertisements

Manish Pandit wrote:
> Couldnt help but ask - Why go through all this to convert a string to a
> double? You can do the same by new Double(yourString).doubleValue() or
> Double.valueOf(yourString).doubleValue().
>
> Is this that you are trying to parse out a double from an alphanumeric
> string, or you need the double in a special format (like with a
> lowercase e) ?
>

It is user input. I need to (for my own piece of mind) be sure that the
user has input "a double, a whole double, and nothing but a double";-)

Most of the internal routines didn't seem to fit the bill. Lower case
"e" was just [to me] a user-friendly allowance.

You should be okay if the user enters a 'e' or an 'E'. For example,
Double.valueOf("1.23456789e9").doubleValue() will return 1.23456789E9
and so will Double.valueOf("1.23456789E9").doubleValue().

Maybe I am missing something again..

Also, just a tip regarding null-proofing the blank check for the string
- you might want to check for null before trimming it - more like if (
S !=null && S.trim().length() >0 ){ .... }

-cheers,
Manish

Richard F.L.R.Snashall wrote:
> Manish Pandit wrote:
> > Couldnt help but ask - Why go through all this to convert a string to a
> > double? You can do the same by new Double(yourString).doubleValue() or
> > Double.valueOf(yourString).doubleValue().
> >
> > Is this that you are trying to parse out a double from an alphanumeric
> > string, or you need the double in a special format (like with a
> > lowercase e) ?
> >
>
> It is user input. I need to (for my own piece of mind) be sure that the
> user has input "a double, a whole double, and nothing but a double";-)
>
> Most of the internal routines didn't seem to fit the bill. Lower case
> "e" was just [to me] a user-friendly allowance.

Manish Pandit wrote:
> Hi,
>
> You should be okay if the user enters a 'e' or an 'E'. For example,
> Double.valueOf("1.23456789e9").doubleValue() will return 1.23456789E9
> and so will Double.valueOf("1.23456789E9").doubleValue().
>
> Maybe I am missing something again..
>

I was using Double.parseDouble. According to the documentation:

"static double parseDouble(String s)
Returns a new double initialized to the value represented by
the specified String, as performed by the valueOf method of class Double."

That failed to work properly when the user input "e".
> Also, just a tip regarding null-proofing the blank check for the string
> - you might want to check for null before trimming it - more like if (
> S !=null && S.trim().length() >0 ){ .... }

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!