• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rounding a value in jsp by using JSTL Expressions

 
venu babu.k
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

This is venu,


Iam trying to round a double value in a jsp page with jstl expression, but it is showing error message.
If any body knows how to do please give suggestion me (or) any small example.

For example:

<input type="text" tabindex="2" value='<cut value="${Obj.Value}"></cut>' />

Here in above example i want to round of the value the Obj.Value

I tried with ${Math.round(Obj.Value)} but no use.



Thank you in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things:

1) Never say "I got an error". Show the error message.

2) Red text on tan is unreadable. Don't use color needlessly.

3) Be sure to disable smilies when posting code.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
venu babu.k wrote:I tried with ${Math.round(Obj.Value)} but no use.

Of course not. You can't just call methods with the EL.

The easiest thing to do, because the round() method is already a static method, is to define it as an EL function in a TLD. The you can use it from the EL easily.

 
Stefan Evans
Bartender
Posts: 1692
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another potential way to do it is to use the <fmt:formatNumber> tag



It is almost equivalent to Math.round, but not quite. Math.round method always rounds a .5 up to the nearest whole number.
The number formatter uses the ROUND_EVEN approach, meaning if it is rounding .5, it will round to the nearest EVEN number (may be up or down)


One thing that bugs me looking at this code, is that you are putting a rounded result into an input field.
I'm of the opinion that if the value for that field is meant to be an integer, then Obj.Value should be an integer.
If this page is used for an edit screen, the value can change without user intervention because of your rounding here.
However you don't have a name attribute on this input field, so I guess you're not using it for submitting the page. Why then are you using <input type="text">??

 
venu babu.k
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks

For your suggestions,from next time i will follow the Conventions.I will try those 2 scenarios.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic