This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "convertNumber and rounding mode" Watch "convertNumber and rounding mode" New topic
Author

convertNumber and rounding mode

Johnny Caimbridge
Greenhorn

Joined: Nov 15, 2010
Posts: 8
Hello,

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.

Thanks
Johnny Caimbridge
Greenhorn

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: 15962
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Johnny Caimbridge
Greenhorn

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: http://aspose.com/file-tools
 
subject: convertNumber and rounding mode
 
Similar Threads
convertNumber question
How do I NOT require currency symbol on input, but still display it with convertNumber?
Problem using convertNumber for decimal numbers
Math , round number
convertNumber and currencySymbol