Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2007-11-16
6
1,713 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
java stored proc example 9 44
String array comparison 4 38
learn programming 8 30
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

789 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