permaculture playing cards*
The moose likes Struts and the fly likes bean:write format string as number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "bean:write format string as number" Watch "bean:write format string as number" New topic
Author

bean:write format string as number

Damian Lopez
Greenhorn

Joined: Jul 26, 2005
Posts: 17
Hi, in a jsp i am displaying some numeric bean properties (all of them are strings) and i would like to know how could i apply a format to them.
The problem is that
<bean:write name="bean" property="p" format="###.##"/>
does not work because it is a string so it doesn�t get formatted, any ideas?
Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest adding a second getter for the field that converts the String to a number. Something like this:



Then in your JSP you can code:
<bean:write name="bean" property="fooAsFloat" format="###.##"/>


Merrill
Consultant, Sima Solutions
Damian Lopez
Greenhorn

Joined: Jul 26, 2005
Posts: 17
Thanks, finally i am using new DecimalFormat(String x).format(float y) to format all the properties before refreshing the jsp
K Sathya Narayanan
Greenhorn

Joined: Feb 15, 2007
Posts: 28
hi
i used the below code but it is not getting formatetd

<bean:write name="bandwidthModel" property="bytes" format="<%=new DecimalFormat("####,##").format(0)%>" /></td>



please suggest me a code that can format the

<bean:write name="bandwidthModel" property="bytes">

in this "####,##" format

thanks in advance
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You're working too hard. The format attribute should contain a simple String indicating how to format the number. Change your code to:

Just make sure that the bytes property contains a number (float, double, etc.) rather than a String or other data type. If it doesn't, convert it to a numeric data type in the getter as I demonstrated in a previous post.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: bean:write format string as number
 
Similar Threads
Date and Dollar amounts in Form Beans
Designing an Airline ActionForm with a Collection of Aircraft
struts bean:write tag with format key
<bean:write/> of struts1
Date formats