I'm trying to do a very basic operation as follows:
double a=21/5;
System.out.println(a);
However, each time I get 4.0 as the output and not 4.2. I'm encountering this for the first time. I've been using Java for years, but never came across this obscurity.
You are using integer division, which result will always be integer You should use something like this.
double a=(double)21/5;
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