Parsing String to int

Discussion in 'Java' started by Bruintje Beer, Apr 19, 2008.

  1. Hi,

    Does anyone know how to convert a String to an int using format below.

    example

    String s = "123,45"; (note a comma as separator)

    The int value must have the value 12345;

    John
     
    Bruintje Beer, Apr 19, 2008
    #1
    1. Advertising

  2. "Bruintje Beer" <> wrote in message
    news:4809d5ab$0$24401$...
    > Hi,
    >
    > Does anyone know how to convert a String to an int using format below.
    >
    > example
    >
    > String s = "123,45"; (note a comma as separator)
    >
    > The int value must have the value 12345;
    >
    > John


    If this is the only use case you'll ever have, do what Matt suggested, and
    document it. Otherwise consider java.text.DecimalFormat if other
    internationalization issues creep in.

    AHS
     
    Arved Sandstrom, Apr 19, 2008
    #2
    1. Advertising

  3. Bruintje Beer

    Roedy Green Guest

    Roedy Green, Apr 19, 2008
    #3
  4. Bruintje Beer

    Arne Vajhøj Guest

    Bruintje Beer wrote:
    > Does anyone know how to convert a String to an int using format below.
    >
    > example
    >
    > String s = "123,45"; (note a comma as separator)
    >
    > The int value must have the value 12345;


    One way:

    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    NumberFormat fmt = new DecimalFormat("0,0", sym);
    int iv = (int)(long)(Long)fmt.parse(s);

    Arne
     
    Arne Vajhøj, May 4, 2008
    #4
    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. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    481
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    1,031
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    464
    James Kanze
    Apr 17, 2007
Loading...

Share This Page