• Post Reply Bookmark Topic Watch Topic
  • New Topic

Division not working for me  RSS feed

 
Terry Chambers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has to be a simple answer to this ...





When value of "number" displays, it is always 0.0, even if I replaced "Served" and "Max" with the actual numbers.

Why is it not printing the value of the division?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because integer division always leads to an integer number. The remainder simply gets chopped off. Only if at least one of the two operands is a float or a double will you keep the remainder.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, each operand is widened to the type of the widest operand (or int if all operands are narrower than int), and that is the type of the result. The result can then be assigned to a variable of a wider type without an explicit cast.

See JLS 5.6.
 
Terry Chambers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!