Yes, it's been a while since I did Java, and I've no idea where that +1 came from. It should actually look like:
Code:
String num = (new Float(myfloat)).toString();
int placesBefore = num.indexOf((int)'.'),
placesAfter = num.length() - placesBefore - 1;
Can u explain to me what indexOf does
Read the API documentation for java.lang.String.
and why i might of needed substring?
I'm not entirely sure. It's possible that you were expected to do:
Code:
String beforeDecimal = substring(0, placesBefore);
int afterDecimal = substring(placesBefore + 1);
int placesAfter = afterDecimal.length();
... but that's a terribly inefficient way of doing it. To accomplish the same effect (but this is still inferior if it's only necessary to find the number of places), one could do:
Code:
String[] decimalSides = num.split("\.");
int beforeDecimal = decimalSides[0],
afterDecimal = decimalSides[1];
Bookmarks