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
Add a comment
|
2 Answers
int i = (int)Float.parseFloat("1.0");
int j = Float.valueOf("1.0").intValue();
1 Comment
Steve Kuo
Looks good, the only subtlety being whether it tunicates or rounds.
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-