Solved

Quick and (hopefully) simple float question

Posted on 2004-10-25
181 Views
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">
<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?
0
Question by:Greystoke
    3 Comments
     
    LVL 35

    Accepted Solution

    by:
    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
     
    LVL 13

    Expert Comment

    by:petmagdy
    or just:    
    float aTest = 90.0/60.0;
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Suggested Solutions

    Phishing is at the top of most security top 10 efforts you should be pursuing in 2016 and beyond. If you don't have phishing incorporated into your Security Awareness Program yet, now is the time. Phishers, and the scams they use, are only going to …
    Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now