Solved

how to format a number as ###,### in JSP/Java

Posted on 2007-11-16
6
1,711 Views
Last Modified: 2013-11-23
How to format a number as ###,###

I have the following code


 <%
    DecimalFormat df = new DecimalFormat("###,###");
    long MySourceBytes = df.format((MyList.get(i).getSourceBytes())/1024);
 %>

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\Catalina\localhost\NFD\org\apache\jsp\view2_jsp.java:644: incompatible types
found   : java.lang.String
required: long
            long MySourceBytes = df.format((MyList.get(i).getSourceBytes())/1024);
                                          ^

How to fix this problem?
0
Comment
Question by:wasabi3689
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20303317
df.format returns a string.  Why do you format it if you are going to convert it to a long?

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.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 20305380
Try this:
 <%
    DecimalFormat df = new DecimalFormat("###,###");
    String MySourceBytes = df.format((MyList.get(i).getSourceBytes())/1024);
 %>
 ...
 I have <%=MySouceBytes%> Bytes!!
0
 

Author Comment

by:wasabi3689
ID: 20305601
MySourceBytes must be long from get function, now I need to convert MySourceBytes long into string value, then df it
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wasabi3689
ID: 20305655
or
can I directly format a long value output as "###,###"?
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 50 total points
ID: 20305796
OK, so you need:
      <%
          DecimalFormat df = new DecimalFormat("###,###");
          long mySourceBytes = (MyList.get(i).getSourceBytes())/1024; // Store the value in a long
       %>

       ....
       I have <%=df.format(mySourceBytes);%> bytes!!
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20305833
>>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.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now