?
Solved

parseDouble parameters

Posted on 2003-03-05
4
Medium Priority
?
285 Views
Last Modified: 2008-02-01
I'm using parseDouble to convert String amounts to a double format. My string has commas as decimal separators (i.e 1,000,000.00) parseDouble throws an error when trying to convert the numbers with commas. Is there a way to tell parseDouble to ignore the commas or do I have to remove the commas from my string prior to sending it to parseDouble?
0
Comment
Question by:ahamel77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 300 total points
ID: 8072972
 Try this:

try
{
    NumberFormat formatter = new DecimalFormat("#,###,###.##");
    Number s = formatter.parse("1,234,000.56");
    System.out.println(s.doubleValue());
}
catch (ParseException pe)
{
    System.out.println(pe);
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 8072979
 You have to import the java.text.*; package for the above to work.
0
 
LVL 2

Expert Comment

by:haydes007
ID: 8073113
The sites below are the best that can be found for explaining every class in the Java language.


http://java.sun.com/j2se/1.3/docs/api/index.html SDK 1.3 documentation
http://java.sun.com/j2se/1.4/docs/api/index.html SDK 1.4 documentation
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8110100
If removing the commas is OK with you, then do this:


// assuming 'str' is the string that contains the string-form of the double number
// Use a temporary StringBuffer object for removing the commas

StringBuffer sb = new StringBuffer ( str ) ;

for ( int i = 0 ; i < sb.length () ; i ++ )
  if ( sb.charAt ( i ) == ',' )
  {
    sb.deleteCharAt ( i ) ;
    i -- ; // not needed as there won't be successive commas, but no harm in putting this statement

  } // end if, for

double doubleVal = Double.parseDouble ( sb.toString () ) ;


This will remove all commas from the StringBuffer object and then pass its string-equivalent to parseDouble (). This way, 'str' will retain its original value (including commas). If you want it to hold the new value, then you can do:


str = sb.toString () ;
double doubleVal = Double.parseDouble ( str ) ;


Hope that helps!

Mayank.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question