Click to See Complete Forum and Search --> : Rounding Off to two decimal places..


rajeshonline
08-27-2008, 01:40 AM
Hi All,

There is a piece of code where we display a value in one of the column's of the table..for eg
document.getElementById("XYZ").cells[1].innerText== <%=Float.parseFloat("" +1234.7001)%>;

However in the JSP page only 1234.0 is displayed..Does anyone know how to display upto 2 decimal places i.e 1234.00 ?

PS:Have tried DecimalFormat,Big decimal and a javascript function called formatNumber...Nothin is workin in this..

rnd me
08-27-2008, 03:52 AM
dont use parseFloat, it gives up to 16 places...

Number(1234.7001).toPrecision(2)

Fang
08-27-2008, 06:02 AM
Use toFixed not toPrecision: http://www.pageresource.com/jscript/j_a_03.htm

rajeshonline
08-28-2008, 03:22 AM
Hi All,

As you would have noticed <%=Float.parseFloat("" +1234.7001)%> this is a java code and not javascript.

morever if i dont use Float.parseFloat...for eg

document.getElementById("totalAmountRow").cells[1].innerText = <%=1234.7001%>

Then also value displayed is 1234.7..any solutions...

Khalid Ali
08-28-2008, 08:21 AM
you should be able to use
java.text.DecimalFormat df = new DecimalFormat(".00");
df.format (Double.parseDouble("1234.7001"));