I’m looking for a function that does what the heading says.
I ended up with writing code that parses through every character and making another string then Double.parseDouble. Which threw StringIndexoutOfBounds exception. Which is clearly not the way to go.
The block of code is here
int i = 0;
char c = q.charAt(i);
if (Character.isDigit(c)) {
try {
String h = "" + c;
while (Character.isDigit(c) || c == '.') {
if (i == (q.length() - 1)) if (Character.isDigit(q.charAt(i + 1)) || (q.charAt(i + 1) == '.')) {
Log.i("CalcApps", "within while");
h += q.charAt(i + 1);
}
i++;
c = q.charAt(i);
}
result = Double.parseDouble(h);
} catch (Exception e) {
Log.i("MyApps", "Index is out of bounds");
}
}
You should use DecimalFormat and avoid creating your own implementation of a parser. See this question for some examples.