If you are really using constants like this, you could just say:

    int i = (int) ((4. / 8.) * 100.);

and end up with 50.  The decimal points make each of these constants a floating
point number, so all the arithmetic is done in floating point before the final
conversion to int.  In real life, some or all of the terms will be variables --
just make sure they are floating point, as Larry suggests below.

Craig


Larry Fluckiger wrote:

> Nicholas Barrington wrote:
> > Float f = 4 / 8;
> > Integer i = f * 100;
> > Integer i = ((float)(4 / 8)) * 100;
>
> This code won't compile. Even if the java compiler could compile it, it
> would still produce the wrong answer for the same reason. This is because 4
> / 8 is still 0, so f would contain 0.0, and i would still be 0.
>
> The concept is correct that floating point arithmetic needs to be used.
> Binary operators like / and * will convert their operands if they need to.
> So, if both operands are integer, java will do integer math, but if one is a
> float and the other is integer, java will convert the integer to a float and
> perform floating point arithmetic.
>
> Try this instead:
> <%=(int)(((double)4)/8*100)%>
>
> I know this is more complicated, but it works. It converts 4 into a float,
> so the / has to convert the 8 into a float as well. The result of a float
> divide is a float, so the * has to convert its other operand, 100 to a
> float. The result is a float 50, which you may want to convert back to an
> int with the (int) cast. A simpler way to accomplish this is to just store
> the 4 or 8 in a float variable.
>
> Larry
>
> -----Original Message-----
> From: Nicholas Barrington [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, November 24, 1999 4:13 PM
> To: [EMAIL PROTECTED]
> Subject: Re: <%=numresponses/numrequests*100 %>
>
> James,
>
> 4 / 8 *is* 0, at least the integer part is 0, with a remainder (modulus) of
> .5.  Some compilers try to compensate for humans using incorrect data types
> in arithmetic calculations by rounding based on the 0.0 to 0.4 round down,
> 0.5 to 0.9 round up method.  Some don't; they assume we code what we mean
> and only specifiy integers if we are interested in integer precision.  If
> you need to divide numbers and want more accuracy in your results, try using
> float or double style data types for your arithmetic.
>
> E.G.
>
> Float f = 4 / 8;
> Integer i = f * 100;
>
> or
>
> Integer i = ((Float)(4 / 8)) * 100;
>
> -----Original Message-----
> From: Griggs, James [ mailto:[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]> ]
> Sent: Thursday, November 25, 1999 5:25 AM
> To: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> Subject: <%=numresponses/numrequests*100 %>
>
> Any reason the following statement would always produce a 0?
>
> Other expressions work just fine when the result of the first calculation is
> above 1  (eg:  5+2*100).  Is this a rounding issue ???
>
> <%=4/8*100 %>
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to