Solved

# Number Formatting Question

Posted on 2006-11-28
1,200 Views

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();
}
form.setList(list);

JSP Code is:
<nested:iterate id="el" property="list">
<nested:write property="el"/>
</nested:iterate>
0
Question by:tmonteit

LVL 6

Expert Comment

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

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;
}

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

ID: 18029569

DecimalFormat formatter = new DecimalFormat("###.##");
}
0

LVL 6

Expert Comment

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

reach2piyush earned 400 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

Question has a verified solution.

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

### Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.