Solved

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

Posted on 2007-11-16
6
1,715 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 67
Java array 21 142
Java ArrayList and if statement 2 52
junit initializtion error 2 17
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

738 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