Double.parseDouble("100a"); // getting the error as expected
But why does the below line doesn't gets the error
Double.parseDouble("100d");
Please explain me.
According to the source code, the Double.parseDouble(String s) method uses the FloatingDecimal.readJavaFormatString(String s) to parse the provided argument.
There, you can see that there's a check if the provided String ends with 'D', 'd', 'F' or 'f' and if it doesn't, then a NumberFormatException is thrown:
if (i < l &&
((i != l - 1) ||
(in.charAt(i) != 'f' &&
in.charAt(i) != 'F' &&
in.charAt(i) != 'd' &&
in.charAt(i) != 'D'))) {
break parseNumber; // go throw exception
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With