?
Solved

Number Formatting Question

Posted on 2006-11-28
5
Medium Priority
?
1,204 Views
Last Modified: 2013-11-24

I have a servlet that returns an ArrayList of floats to a jsp.   These floats represent percentages and print by default values like .02777777778 and .333333333.    

1.  I would like to format these to 2 decimals.  eg 2.78% and 33.33% respectivlely.  Question 1 is how to do that?

2.  Where is the appropriate place to format these? in the Servlet or JSP?

[Servlet] ----form----> [JSP]

my servlet code looks like:
   ArrayList list = new ArrayList();
    while(xxx){  loop and add #'s
        list.add(new Float( a/b));
    }
   form.setList(list);


JSP Code is:
<nested:iterate id="el" property="list">
   <nested:write property="el"/>
</nested:iterate>
0
Comment
Question by:tmonteit
[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
5 Comments
 
LVL 6

Expert Comment

by:SamsonChung
ID: 18029266
This might be of use to you

http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

Also, do it in the Servlet. The less information you are passing between the pages the better...

0
 

Author Comment

by:tmonteit
ID: 18029486
Just so I'm straight how does it fit into my servlet code?

Say my form looks like this.

class myForm {
  private Float myData;
  // add getters / setters
}

In servlet;

  // How does the decimal formatter fit into here?
  // the form expects a Float, not a DecimalFormatter

  Float foo = 2.987373;
  form.setMyData(foo);

 
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18029569
How about to add Strings into List instead of Float obj?

DecimalFormat formatter = new DecimalFormat("###.##");
while(xxx){  //loop and add #'s
    list.add(formatter.format(100 * a/b) + "%");
}
0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 18030568
Float foo = 2.987373;
String pattern = "%";
DecimalFormat myFormatter = new DecimalFormat(pattern);
foo = myFormatter.format(foo);
form.setMyData(foo);
0
 
LVL 8

Accepted Solution

by:
reach2piyush earned 1600 total points
ID: 18033777
It is done for display purpose(view), so that is the place it should be
either in your form bean or JSP

here are the choices:
 - in form bean, change your getter to give you the truncated value(java.text.NumberFormat or DecimalFormat)
 - in form bean, add another getter to give you the truncated value(java.text.NumberFormat or DecimalFormat)
 - in JSP. use JSTL Format Library <fmt:formatNumber maxFractionDigits=” 2”> {number}</fmt:formatNumber>
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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…
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

770 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