Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Number formatting using JSTL or otherwise  RSS feed

 
Jhakda Velu
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Greetings of the day

I have an input field in my JSP in which user can enter a number. I need to format the number in two ways depending on certain condition

1. Show in percentage format ie, if the user enters 42.5, it should appear as 42.5% (% symbol appended automatically)
2. Show in minutes and seconds format, if the user enters 42.5, it should appear as 42:50 (: appears instead of (.)decimal)

The input field maps to a decimal type in the DB.

I've tried looking at the JSTL format tag, but did not find anything out of the box.

I am thinking on the following lines
1. Write my own tag, which does the needful.
2. Write a Javascript function called on change of the filed to do the necessary setting.

I am using Spring and currently, the input field maps to a Double value in the Command(form equivalent of Struts), so i believe i will have to add another string attribute in the command and set the value to it instead of the current Double value. I will then have to amend the setter function to extract the double value and set it to the existing Double attribute.

Any other approach that fellow Ranchers can suggest is welcome and will be appreciated.

Jhakda
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No JSP mechanism os going to help you with formatting the number on the client. If you want to format a number when creating the HTML page in the first place, the JSTL tag for formatting numerics is certainly the way to go. But when responding to client events, such as data entry on the client, you'll need to use JavaScript.
 
Jhakda Velu
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Thanks Bear for the reply. Yes, I missed the basic point here, change has to happen on user input, so JSTL won't help. JavaScript is the way out. Thanks again for showing the way.

Jhakda
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!