Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convertNumber and rounding mode

 
Johnny Caimbridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Johnny Caimbridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic