Discussions

Web tier: servlets, JSP, Web frameworks: weird display of a float

  1. weird display of a float (1 messages)

    hi,

    i do have a float field in a database. then i display it in my jsp file.


    for a value that is 0.56174 in the database, the display result in the jsp is : 0.5617400000000000
    for a value that is 0.5213 in the database, the display result in the jsp is : 0.52129999999999999
    and for a value that is 5.47620370956691E-03 in the database, the display result in the jsp is : 5.4762037095669111E-3


    as you can see, it's each time different.
    HERE are the different steps i make to display this value under my jsp, so you will maybe find the moment where i don't do the right thing :

    first i do have a String (called here myString) where i put this value :
         myObj.myString = rs.getString("RAT_EOLY");
    then i need to test this :
         if (myObj.myString.equals(""))
    then i forward the object to the jsp this way :
     
         request.setAttribute("searchResult",myObj);
         RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher("/myJSP.jsp");
    finally i display the result in my jsp this way :
         ResponseObj rObj =(ResponseObj)request.getAttribute("searchResult"); // gets the response object of the query

         <% if(rObj != null) { %>
         <% if(rObj.myString!= null) { %>
         <%= rObj.myString.toString() %>
         <% }} %>"> // to display it
    here is a small solution that could maybe works : if i use a primitive float, then i put the database value with rs.getFloat in stead of rs.getString, then ??? then i must test if the float is null/empty. and here i am blocked, i don't know how i could test it. and i don't even know if this would change my display problem in my jsp.

    any idea ?

    Threaded Messages (1)

  2. weird display of a float[ Go to top ]

    i've found the solution, i put it down for other people, it could help :

    String tempString;
    double tempDouble;
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(25);
    tempString = rs.getString("RAT_EOLY");
    if ( tempString == null || tempString.equals("") ) {
    myRespObj.EOLY = "";
    } else {
    tempDouble = Double.parseDouble(tempString);
    myRespObj.EOLY = nf.format(tempDouble);
    }