this is my first question asked in this forum, so if question is novice please advice.
I receive the user input through EditText
, hence it is a charsequence/string. then i created BigDecimal
to hold that value. it handles exponents also gracefully without any trouble. but problem is when user enters values like 1.1E+
(without any trailing exponent value), code crashes in this piece of code. but it works when input is like this 1.1E+2
EditText edit_text_left;
Editable editable_val_text;
....
editable_val_text = edit_text_left.getText();
BigDecimal val = new BigDecimal((editable_val_text.toString().trim())); // crash happens here
how to handle this?
More info: you can try this in google unit conversion app, by choosing Digital Storage : petabyte to bits conversion. and type 9 in petabyte it will show 8.106e+16
in bits section. now try editing 8.106e+16
to 8.106e+
and it will still work. i want similar handling only.
thanks to this community and all who replied. one of the hint gave me an idea to resolve the issue. there is no inbuilt method to handle that ending e/E/e-/E-/e+/E+. rather i made use of String.endsWith method to identify lonely e/E/e-/E-/e+/E+ and removed those from the string.
if ((str.endsWith("e")) || (str.endsWith("E"))) {
str1 = str.copyValueOf(str.toCharArray(), 0, str.length() - 1);
}
else if ((str.endsWith("e+")) || (str.endsWith("E+")) || (str.endsWith("e-")) || (str.endsWith("E-"))) {
str1 = str.copyValueOf(str.toCharArray(), 0, str.length() - 2);
}