Meaningless Drivel is fun!*
The moose likes JSP and the fly likes Rounding a value in jsp by using JSTL Expressions 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 » Java » JSP
Bookmark "Rounding a value in jsp by using JSTL Expressions" Watch "Rounding a value in jsp by using JSTL Expressions" New topic
Author

Rounding a value in jsp by using JSTL Expressions

venu babu.k
Greenhorn

Joined: Nov 09, 2010
Posts: 8
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

Joined: Jan 10, 2002
Posts: 60810
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

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

Joined: Jul 06, 2005
Posts: 1016
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

Joined: Nov 09, 2010
Posts: 8

Thanks

For your suggestions,from next time i will follow the Conventions.I will try those 2 scenarios.
 
Don't get me started about those stupid light bulbs.
 
subject: Rounding a value in jsp by using JSTL Expressions
 
Similar Threads
how to remove special characters in a string using pattern matching
Data truncation: Truncated incorrect DOUBLE value:
WA #1.....word association
using EL to get the keySet of a HashMap
hey i have passed SCJP 6.0 with 95%... thanks to kathy sierra....