Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

parseDouble parameters

Posted on 2003-03-05
4
Medium Priority
?
294 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
  • 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

579 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