Quick and (hopefully) simple float question

Posted on 2004-10-25
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" "">
      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
    LVL 35

    Accepted Solution

    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

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

    Expert Comment

    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 ;-)

    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