wood burning stoves 2.0*
The moose likes JSP and the fly likes unwanted integer division 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 » JSP
Bookmark "unwanted integer division" Watch "unwanted integer division" New topic
Author

unwanted integer division

deb platt
Ranch Hand

Joined: Jan 23, 2008
Posts: 44
This code:


Displays this in the browser:

7325 / 25600 0.000

I want it to print out this:

7325 / 25600 0.286

I can coerce a double result by doing this:



But I figure there must be a better way.

As an aside, the numerator and denominator that are being treated as integers were actually doubles when last seen leaving the server.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Originally posted by deb platt:
As an aside, the numerator and denominator that are being treated as integers were actually doubles when last seen leaving the server.

They never leave the server.

For completeness, please show the code where you set the scoped variables.
[ December 16, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
deb platt
Ranch Hand

Joined: Jan 23, 2008
Posts: 44
num and denom were the result of a number of mathematical operations. However the initial operands were real numbers selected from a database query. So when I said that the numbers were real numbers when they left the server, I meant that the queried numbers were real numbers in the database server.

Here's the code that set num and denom:



To set any of the variables ending with the suffix "sum" I began with a statment such as this:

Perhaps I should have initialized it to the value "0.0".

Then there is a loop where I am accumulating the sum from a query, like this:


In the above snippet, the numbers in row[3] and row[4] should be real numbers.

In most cases the ratio I am seeking works out, but in those cases where the numerator and denominator coincidentally do not have a fractional part, I get integer division, and the ratio is misleadingly reported to be zero.
[ December 17, 2008: Message edited by: deb platt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unwanted integer division
 
Similar Threads
Doubt in c:set tag
How do I call JavaBean set methods from jsp?
Struts and javascript
JSTL Out put problem
[JSTL] how can I convert values (string to int) in JSTL