wasabi3689
asked on
how to format a number as ###,### in JSP/Java
How to format a number as ###,###
I have the following code
<%
DecimalFormat df = new DecimalFormat("###,###");
long MySourceBytes = df.format((MyList.get(i).g etSourceBy tes())/102 4);
%>
Now I have the following error
An error occurred at line: 367 in the jsp file: /view2.jsp
Generated servlet error:
C:\Documents and Settings\ezchen\.netbeans\ 5.5\apache -tomcat-5. 5.17_base\ work\Catal ina\localh ost\NFD\or g\apache\j sp\view2_j sp.java:64 4: incompatible types
found : java.lang.String
required: long
long MySourceBytes = df.format((MyList.get(i).g etSourceBy tes())/102 4);
^
How to fix this problem?
I have the following code
<%
DecimalFormat df = new DecimalFormat("###,###");
long MySourceBytes = df.format((MyList.get(i).g
%>
Now I have the following error
An error occurred at line: 367 in the jsp file: /view2.jsp
Generated servlet error:
C:\Documents and Settings\ezchen\.netbeans\
found : java.lang.String
required: long
long MySourceBytes = df.format((MyList.get(i).g
^
How to fix this problem?
Try this:
<%
DecimalFormat df = new DecimalFormat("###,###");
String MySourceBytes = df.format((MyList.get(i).g etSourceBy tes())/102 4);
%>
...
I have <%=MySouceBytes%> Bytes!!
<%
DecimalFormat df = new DecimalFormat("###,###");
String MySourceBytes = df.format((MyList.get(i).g
%>
...
I have <%=MySouceBytes%> Bytes!!
ASKER
MySourceBytes must be long from get function, now I need to convert MySourceBytes long into string value, then df it
ASKER
or
can I directly format a long value output as "###,###"?
can I directly format a long value output as "###,###"?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>can I directly format a long value output as "###,###"?
No -- long is a long, which is a binary type. It doesn't have a string display format. That's why there are classes like DecimalFormat, for formatting binary number types like long, when you want to display the value of a long variable in a String format.
No -- long is a long, which is a binary type. It doesn't have a string display format. That's why there are classes like DecimalFormat, for formatting binary number types like long, when you want to display the value of a long variable in a String format.
If you want to turn the string into a long, then something like
Long.parseLong( aString )
works.
You'll have to take the comma outfor parseLong -- you might want to use the DecimalFormat parse method to convert the string back to a number.