If I were going to support the floating point suffix F, I would do it like this:
if (str.endsWith("F") || str.endsWith("f")) { num = Double.parseDouble(str.substring(0, str.length() - 1)); } else { num = Double.parseDouble(str); }
However, I would recommend that you do not allow this.
The suffix "f" is not a standard mathematical / scientific form. It only has a certain meaning in some programming languages ... as a numerical literal in the source code of a program. In this context, it denotes a floating point number with an IEE single precision representation. (And you are trying to treat it like a double !)
The best way to avoid this potential confusion (for small subset programmers who don't understand the difference between a number and a numeric literal) is to simply treat it as bad input.
However, is there any other alternative api for it.
AFAIK, no. Mostly because the format really doesn't make sense.
source share