1

How can I convert "1,000" (input obtained as a string) to an integer?

1

11 Answers 11

7
    DecimalFormat df = new DecimalFormat("#,###");
    int i = df.parse("1,000").intValue();
    System.out.println(i);
Sign up to request clarification or add additional context in comments.

1 Comment

A generic approach colud be like this. Integer.parseInt("1,000".replaceAll("[^\\d.]", ""))
3
Integer i = Integer.valueOf("1,000".replaceAll(",", ""));

2 Comments

better : System.out.println(Integer.parseInt("1,000".replaceAll("\\D+", "")));
@Peter Lawrey: It's programming with a positive attitude ;)
2
String stringValue = "1,000";

String cleanedStringValue = stringValue.replace(',','');

int intValue = Integer.parseInt(cleanedStringValue);

2 Comments

Becuase for some reason it was not rendering the code properly when the comments were included! Pardon me but Im new to all of this stackoverflow stuff!
Look at 3rd revision here: stackoverflow.com/posts/5470551/revisions which is made by me. It shows comment and code both well.
1

ParseInt:

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29

1 Comment

Does not work. See documentation "The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value."
1

I'd take a look at this class: http://download.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html

Comments

0
String str ="1000";

Try this

 int i = Integer.valueOf("1,000".replaceAll(",", ""));

Comments

0
Integer.parseInt("1000");

Prefer avoiding "magic numbers"

String num = "1000";
Integer.parseInt(num);

Comments

0

Use code like the following.

String str = "1000";
int result;
try {
    result = Integer.parseInt(str);
} catch(NumberFormatException ex) {
    System.err.println("That was not an integer");
}

Comments

0

Just replace all comma with empty string and then use convert.ToInt32();

string str = "1,000";
int num = Integer.parseInt(str.replace(",",""));

2 Comments

I need to perform the conversion in java!
@user673218 - This is available in java too ;)
0

with a comma (,) in the the string you probably cannot do it.

Comments

0

Always prefer to use Long.ValueOf instead of new Long. Indeed, the new Long always result in a new object whereas Long.ValueOf allows to cache the values by the compiler. Thanks to the cache, you code will be faster to execute.

1 Comment

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.