File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes convertNumber and rounding mode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "convertNumber and rounding mode" Watch "convertNumber and rounding mode" New topic

convertNumber and rounding mode

Johnny Caimbridge

Joined: Nov 15, 2010
Posts: 8

How would I go about specifying the convertNumber tag's rounding mode? The default "round half even" mode is not suitable for my application. It would seem kind of silly to write an entirely new converter for something like this. If this is actually necessary, how could I extend the existing converter instead of writing one from scratch?

I cannot round the data beforehand as it cannot be tainted, and maintaining parallel copies of the data (ie "real" and "rounded") would create serious maintenance issues.

Johnny Caimbridge

Joined: Nov 15, 2010
Posts: 8
I just read that the convertNumber tags use "NumberFormat" by default, so I figured I would create a phase listener and just reinitialize it in there.

However, this is not working, and on each call to the phase listener the rounding mode is reset to RoundingMode.HALF_EVEN. I'm ignoring the fact that this may not be thread-safe for the time being.

I have no idea how to set the rounding mode for the actual thread performing the number formatting.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

I had to think about this one. The reason that there is no rounding option, I believe, is that it is not the purpose of the number view/update process to round numbers. That all numbers are, in fact, expected to be "exact". Otherwise, due to the way that JSF works, if you rounded a number being displayed in an inputText control, JSF would destroy the original more precise value.

Don't make the mistake that the backing bean must be a direct reference into the persistent object model or other business structure. Backing beans are DISPLAY models. If you cannot get what you want directly, you should add an appropriate backing bean property and reference it. Then reflect that property value to and from the actual business/persistence property it represents.

I have to do this fairly routinely - for example to bind checkboxes to items in databases too benighted to possess boolean datatypes, and while I may not enjoy it, it works and can be done without any arcane messing around inside JSF - only POJO processes are required.

An IDE is no substitute for an Intelligent Developer.
Johnny Caimbridge

Joined: Nov 15, 2010
Posts: 8
I ended up writing a new converter which inherits from NumberConverter and just applies rounding before handing it off to NumberConverter (unfortunately, Converters don't permit one to pass them parameters--or at least I haven't found a way. I mean, I could have used a ConvertTag, but I couldn't figure out a way to inherit from the existing tag class for NumberConverter without introducing errors).
I agree. Here's the link:
subject: convertNumber and rounding mode
It's not a secret anymore!