Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean:write format string as number

 
Damian Lopez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="###.##"/>
 
Damian Lopez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic