Quick and (hopefully) simple float question

Hello all,

I've never tried to use a float in my JSP pages before, so hope I'm just doing something stupid - anyway, here is the code:

//**********************************************
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.lang.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<%
      float aTest = 90/60;
%>
<%= aTest %>
</body>
</html>
//**********************************************


The output I get on the screen is 1.0
How can I get it to show the correct 1.5?
GreystokeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TimYatesConnect With a Mentor Commented:
because the left and right hand sides are ints, it is doing an int divide, then putting the int result into a float variable...

a way of getting round this is:

<%
     float aTest = (float)90 / (float)60;
%>

So you tell java that both sides are floats, and it does a floating point divide :-)
0
 
petmagdyCommented:
or just:    
float aTest = 90.0/60.0;
0
 
TimYatesCommented:
But that might give you warnings as you will be trying to fit a double into a float...

so:

float aTest = 90.0f / 60.0f ;

would probably be better ;-)
0
All Courses

From novice to tech pro — start learning today.