Solved

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

Posted on 2007-11-16
6
1,710 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 26

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 26

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 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

15 Experts available now in Live!

Get 1:1 Help Now