Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

division evaluates to zero

 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is so stinkin' basic, I'm embarrassed to even post the question, but...

long section1score = Long.valueOf(request.getParameter("score1")).longValue() + Long.valueOf(request.getParameter("score2")).longValue();

double section1perct = (section1score/40)*100;

...Why on earth does this evaluate to zero??? section1score holds a value of 20.

Thanks!
Lisa
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integral division always truncates. If you want a floating-point result, you will need to perform the division with a floating-point type. One way to accomplish this is to simply make the literal 40 a double...

(section1score/40.0)*100;
 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

Goodness, what a silly oversight on my part...

Thanks Again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic