Problem regarding calculation in JSP

The problem with this code is I'm getting the popularity of an author as 0% (I mean zero percent if the number of borrowed books is 14 and the total number of books borrowed of the selected author is 3 - it should be 21.42%). Why is this happening?

All result are correct except the last one:

Author is 0 % popular (for above given data)

<%
String requestedoprations = request.getParameter("popularity");
if("check".equalsIgnoreCase(requestedoprations)){
    int num=LimsHandler.getInstance().popularitycheck(
        request.getParameter("selectedauthor"));
    if(num!=0){
        Limsdetails[] list = LimsHandler.getInstance().libsdetails();
        String totbks=list[0].getTot_books();
        String totbrwdbk=list[0].getTot_borrowed_bks();
        int totbksint=Integer.parseInt(totbks);
        int totbrwdbksint=Integer.parseInt(totbrwdbk);
        float per=(num/totbrwdbksint)*100;          
%>
<font color="brown">
    <b>Total No of Books Available in Library is : <%=totbksint %><br></br>
    Out of which <%=totbrwdbksint %> are borrowed.<br></br>
    <b>No of readers reading Author 
        <%=request.getParameter("selectedauthor") %>'s book. : 
        <%=num %></b><br></br>
    <b> Author <%=request.getParameter("selectedauthor") %> is <%=per %> % 
        popular!</b><br></br>
</font>

<%}else{ %>
    <h4 align="center">
        <font color="red">
            <img border="0" src="images/close.PNG" ><br></br>
            Oops! some error occurred!
        </font>
    </h4>
<%
}
out.flush();
%>

<%} %>

Answers


This isn't really a JSP problem - it's how Java deals with integer arithmetic. The relevant lines are:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;

You're performing an "int / int" division and then multiplying by 100. That will perform the division using integer arithmetic - so the result will be 0. Multiplying 0 by 100 still gives 0.

The easiest way to fix it is to make one of the values a float or double. For example:

int num = LimsHandler.getInstance().popularitycheck(...);    
float totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;

Alternatively, you could cast within the expression:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / (float) totbrwdbksint) * 100;

At this point the division will be performed using floating point arithmetic, and you'll get the answer you expect.


Need Your Help

Backbone.js Why is my default model present in a fetched collection?

backbone.js backbone.js-collections

I'm sure I'm missing something very basic. I'm setting up a collection of fetched objects

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.