Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Quick and (hopefully) simple float question

Posted on 2004-10-25
Medium Priority
Last Modified: 2010-07-27
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">
      float aTest = 90/60;
<%= aTest %>

The output I get on the screen is 1.0
How can I get it to show the correct 1.5?
Question by:Greystoke
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 35

Accepted Solution

TimYates earned 400 total points
ID: 12399083
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 :-)
LVL 13

Expert Comment

ID: 12399142
or just:    
float aTest = 90.0/60.0;
LVL 35

Expert Comment

ID: 12399238
But that might give you warnings as you will be trying to fit a double into a float...


float aTest = 90.0f / 60.0f ;

would probably be better ;-)

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With the evolution of technology, we have finally reached a point where it is possible to have home automation features like having your thermostat turn up and door lock itself when you leave, as well as a complete home security system. This is a st…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question