• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double division stay 0.0  RSS feed

 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This is my first post in the ranch and hope it's the right place to do it.

Here's the strange behavior I would ask for:

the output expected is 0.02
the real output is 0.0!
Why is that?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numeric literals that do not have a decimal point are treated as int values thus the expression 2 / 100 divides two int values. Integer division will produce an integer value and in this case it will be 0. That is the value that gets assigned to your double result variable.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this expression instead: 2.0 / 100.0
 
Daniele Barell
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Numeric literals that do not have a decimal point are treated as int values thus the expression 2 / 100 divides two int values. Integer division will produce an integer value and in this case it will be 0. That is the value that gets assigned to your double result variable.

Thank Junilu for the quick answer!
So the correct literals to use must be double as well:

And it actually gives the expected 0.02 result.

Thanks again!

dan

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, only one of the literals needs to be a double. That is, 2 / 100.0 or 2.0 / 100 would also work. In these cases, the int value would get promoted to a double before the division operation is evaluated. Read up on value promotions for arithmetic operations in Java.
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniele Barell wrote:Hi all,
This is my first post in the ranch and hope it's the right place to do it.

Here's the strange behavior I would ask for:

the output expected is 0.02
the real output is 0.0!
Why is that?


Your code can rewrite like this:

division between 2 to 100 (2/100) equals to 0 because the result of that division is an integer.
After division, casting the result of division is performed (double(0))
And the result is : 0.0
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Ta, OP,


It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.


Might depend on your locale.
en_GB gives "0.0".
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Liutauras Vilda wrote:
It is worth to point out, that OP didn't get 0.0 with his initial code snippet, but rather 0.


Might depend on your locale.
en_GB gives "0.0".

Oh, my mistake, I didn't see the result was assigned to result which is of type double.

In my mind I thought for some reason about the case:


Thanks for correcting me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!