× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On Friday, 10/04/2002 at 12:11 AST, DReid@capitalfactors.com wrote:
> What is the simplest method to convert this string to a double?
>
>             156,250.25-

A little test program will show that Double.parseDouble(String) chokes on
this format.  (Commas and trailing sign being the issue...)

I was all set to suggest "cleansing" the String value (e.g. remove commas
and put any trailing sign character at the front) and then calling
Double.parseDouble(String), until I saw Gary's post suggesting
DecimalFormat.  Clearly, DecimalFormat is *much* simpler to code.

Nice catch, Gary!

One nit.  If I read the javadoc correctly, the parse method of NumberFormat
returns a java.lang.Number object (either a java.lang.Double or a
java.lang.Long), so to get a true, primitive 'double' the code might need
to look like:

double d = df.parse("156,250.25-").doubleValue();

And, since parse(String) throws a ParseException, the expression would
either need a try/catch, or the method declaration would have to include a
"throws ParseException" clause.

-blair




As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.