How to convert a string to a number

Discussion in 'Java' started by chirs, Apr 9, 2004.

  1. chirs

    chirs Guest

    Hi,

    How to convert a string such as "1,000" to a number? I tried
    Integer.parseInt("1,000");. But it did not work.

    Thank you.

    Chris
    chirs, Apr 9, 2004
    #1
    1. Advertising

  2. chirs

    Dave Miller Guest

    In article <>,
    says...
    > Hi,
    >
    > How to convert a string such as "1,000" to a number? I tried
    > Integer.parseInt("1,000");. But it did not work.
    >
    > Thank you.
    >
    > Chris
    >

    Look at java.text.NumberFormat - it can handle extraneous input like
    commas, monetary symbols, etc.
    --
    Dave Miller
    FundablePlans - Create a custom business plan online - only $39.95
    http://www.fundableplans.com
    Dave Miller, Apr 9, 2004
    #2
    1. Advertising

  3. chirs

    Sudsy Guest

    chirs wrote:
    > Hi,
    >
    > How to convert a string such as "1,000" to a number? I tried
    > Integer.parseInt("1,000");. But it did not work.
    >
    > Thank you.
    >
    > Chris


    Not surprising that it didn't work. Commas as part of the number don't
    generally reflect how computers deal with numbers. But there is a class
    designed to handle these situations. Try this:

    import java.text.DecimalFormat;
    ....
    DecimalFormat formatter = new DecimalFormat( "#,##0" );
    String s = "1,000";
    int i = -1;
    try {
    i = formatter.parse( s ).intValue();
    }
    catch( ParseException e ) {
    ...
    }

    Don't you just love the javadocs?
    Sudsy, Apr 9, 2004
    #3
  4. chirs

    Roedy Green Guest

    On 8 Apr 2004 21:46:11 -0700, (chirs) wrote or quoted :

    >How to convert a string such as "1,000" to a number? I tried
    >Integer.parseInt("1,000");. But it did not work.


    It does not like the comma. You will have to filter it out before
    hand.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 9, 2004
    #4
  5. chirs

    Roedy Green Guest

    On Fri, 09 Apr 2004 02:01:47 -0400, Sudsy <>
    wrote or quoted :

    > DecimalFormat formatter = new DecimalFormat( "#,##0" );


    When you do that, what sorts of string are acceptable. Does it insist
    on the comma?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 9, 2004
    #5
  6. chirs

    Sudsy Guest

    Roedy asks:
    >> DecimalFormat formatter = new DecimalFormat( "#,##0" );

    >
    > When you do that, what sorts of string are acceptable. Does it insist
    > on the comma?


    It's actually very forgiving. It seems to completely ignore
    the commas, the number of digits between them, etc. I wrote
    this quick test:

    public class NumberTest {
    public static void main( String args[] ) {
    DecimalFormat formatter = new DecimalFormat( "#,##0" );
    int parsed;
    String formatted;
    for( int i = 0; i < args.length; i++ ) {
    try {
    parsed = formatter.parse( args
    ).intValue();
    formatted = formatter.format( parsed );
    System.out.println( "Original = " +
    args +
    ", parsed = " + parsed +
    ", formatted = " + formatted );
    }
    catch( Exception e ) {
    System.err.println( e.toString() );
    }
    }
    }
    }

    Here's a test run:

    $ java NumberTest 1,000 1000 4,1567,890 4567890 1,34546
    Original = 1,000, parsed = 1000, formatted = 1,000
    Original = 1000, parsed = 1000, formatted = 1,000
    Original = 4,1567,890, parsed = 41567890, formatted = 41,567,890
    Original = 4567890, parsed = 4567890, formatted = 4,567,890
    Original = 1,34546, parsed = 134546, formatted = 134,546
    $

    So it's more powerful on the formatting end than the parsing one.
    Fair enough!
    Sudsy, Apr 9, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,770
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Replies:
    5
    Views:
    522
    Pete Becker
    May 21, 2005
  3. Hako
    Replies:
    13
    Views:
    38,838
    Ron Adam
    Nov 10, 2005
  4. muss
    Replies:
    13
    Views:
    717
    Vladimir S. Oka
    Mar 27, 2006
  5. davidj411
    Replies:
    11
    Views:
    1,296
    Kam-Hung Soh
    May 29, 2008
Loading...

Share This Page