Parsing String to int

B

Bruintje Beer

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
 
A

Arved Sandstrom

Bruintje Beer said:
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
 
A

Arne Vajhøj

Bruintje said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top