Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

weird display of a float

Status
Not open for further replies.

tyris

Programmer
Nov 2, 2000
311
FR
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 :
Code:
    myObj.myString = rs.getString("RAT_EOLY");
Then i need to test this :
Code:
    if (myObj.myString.equals(""))
Then i forward the object to the jsp this way :
Code:
    request.setAttribute("searchResult",myObj); 
    RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher("/myJSP.jsp");
Finally i display the result in my jsp this way :
Code:
    ResponseObj rObj =(ResponseObj)request.getAttribute("searchResult"); // gets the response object of the query
    <% if(rObj != null) { %> // to display it
    <% if(rObj.myString!= null) { %>
    <%= rObj.myString.toString() %>
    <% }} %>&quot;>

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 ?
Best regards X-),
Elise
 
i've found the solution, i put it down for other people, it could help :
Code:
String tempString;
double tempDouble;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(25);
tempString  = rs.getString(&quot;RAT_EOLY&quot;);
if ( tempString == null || tempString.equals(&quot;&quot;) ) {
	myRespObj.EOLY = &quot;&quot;;
} else {
	tempDouble = Double.parseDouble(tempString);
	myRespObj.EOLY = nf.format(tempDouble);
}
Best regards X-),
Elise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top