0

I receive a string from a server that sometimes is a double and sometimes is an integer. And i need it to be an integer. When i do a Integer.parseInt(confusedStringNumber) If its a double, it throws a Number format exception. Is there a way to format the string to drop all decimal places whether they exist or not ? Thanks

0

2 Answers 2

4
int i = (int)Float.parseFloat("1.0");
int j = Float.valueOf("1.0").intValue();
Sign up to request clarification or add additional context in comments.

1 Comment

Looks good, the only subtlety being whether it tunicates or rounds.
0

Refer to the following types and methods which can give you what you're asking for: https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#round-java.math.MathContext-

Comments

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.