Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# what is logic of JSTL fmt tag in formatting decimal numbers

revathi balakrishnan
Greenhorn
Posts: 4
hi ,

We have the below code in our jsp

<fmt : formatNumber MaxFraction Digits="1" MinFraction Digits="1" value=\$variable >

if the variable value is 6.650996 it prints 6.7
if the variable value is 6.652273 it prints 6.7
if the variable value is 6.65 it prints 6.6

we actually expect 6.65 also to be printed us 6.7,since the second decimal digit is 5
It would be very helpful if you could explain us the logic of fmt in converting decimal numbers...

Bear Bibeault
Author and ninkuma
Marshal
Posts: 64633
86
revathi balakrishnan wrote:
We have the below code in our jsp

<fmt : formatNumber MaxFraction Digits="1" MinFraction Digits="1" value=\$variable >

I'm sure that you do not. That code is full of syntax errors that will prevent the JSP from translating.

Please be sure not to waste time with red herrings by posting sloppily. Please post the exact code as it really exists in the code.

we actually expect 6.65 also to be printed us 6.7,since the second decimal digit is 5

Why would you expect that when you told the tag to only use one fractional digit?

Stefan Evans
Bartender
Posts: 1671
10
The fmt format number tag uses the java.text.DecimalFormat class to do its work.
The answer can be found in the Java API for java.math.RoundingMode.

By default it uses HALF_EVEN rounding. It will round to the closest number. If there is a 'tie' for that number, then it rounds up if the previous digit is odd, and down if the previous digit is even.

6.551 --> 6.6
6.55 --> 6.6
6.549 --> 6.5

6.651 --> 6.7
6.65 --> 6.6
6.649 --> 6.6

Paul Clapham
Sheriff
Posts: 20775
30
Stefan Evans wrote:The answer can be found in the Java API for java.math.RoundingMode.

It's buried very deeply in there, then. Searching for the word "default" fails, as does the word "standard". Reading the whole thing from start to finish eventually leads to an obscure paragraph which says that one of the rounding modes is "analogous to the rounding policy used for float and double arithmetic in Java". Googling for "java float rounding policy" leads to the RoundingMode API documentation and to a horrid roseindia "tutorial" but not to any document which tells you that Java does have a rounding policy and what it is.

I spent some time earlier today trying to figure out JSTL's rounding rules with no success. Earlier JSTL specs don't even mention rounding, and later versions just say that it takes place, but not how. I also searched some of the Java API like NumberFormat on the theory that Java might have a default rounding rule. Still no success. And I consider myself to be pretty good at finding information if it's in online documentation. So I gave up and left the question unanswered. I would have to say, though, that it's a much harder question than it should be.

Stefan Evans
Bartender
Posts: 1671
10
NumberFormat was close... I actually found the answer in java.text.DecimalFormat, which is what led me to the java.match.RoundingMode class. DecimalFormat is mentioned in the pattern part of the JSTL 1.2 spec I was reading, so I figured it would also apply.
I guess I skipped all my logic/search on the way by providing the direct link, and also wanting to make use of the shiny forum feature that automatically replaces java class names with links to the API.

Anyway quoting from the java.text.DecimalFormat class:
Rounding
DecimalFormat provides rounding modes defined in RoundingMode for formatting. By default, it uses RoundingMode.HALF_EVEN.

Not the easiest thing to find, but it is there :-)