Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the Double.parseDouble("100d") gets no error

Tags:

java

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.

like image 644
prabhakar Reddy G Avatar asked Dec 06 '25 02:12

prabhakar Reddy G


1 Answers

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
     }
like image 131
Konstantin Yovkov Avatar answered Dec 08 '25 16:12

Konstantin Yovkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!