File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes division evaluates to zero Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "division evaluates to zero" Watch "division evaluates to zero" New topic
Author

division evaluates to zero

Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
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

Joined: Aug 31, 2004
Posts: 11343

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;


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
Thank you!

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

Thanks Again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: division evaluates to zero
 
Similar Threads
need help urgentl like yesterdayy
problem with double data type
the world's smallest number?
Regarding type casting
request.getParameter...not taking Long