Reputation: 1546
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
Upvotes: 0
Views: 68
Reputation: 1629
int i = (int)Float.parseFloat("1.0");
int j = Float.valueOf("1.0").intValue();
Upvotes: 4
Reputation: 3433
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-
Upvotes: 0