0

I don't like the way f:convertNumber display NaN ("\ufffd") and both of the infinities ("\u221e").

Is there a way to extend the out-of-the-box converter in order to inject my own display logic? Thank you.

geca
  • 2,711
  • 2
  • 17
  • 26
  • You could create your own converter. See [Creating a JSF 1.2 Custom Converter with Attributes](http://jerryorr.blogspot.com/2011/10/creating-jsf-12-custom-converter-with.html) and [Create simple custom Converter implementation class in JSF](http://www.javabeat.net/2008/07/create-simple-custom-converter-implementation-class-in-jsf/) – Luiggi Mendoza Jan 28 '13 at 15:07

1 Answers1

1

To do this:

  1. Create a class that extends NumberConverter.
  2. Override the getAsString method by explicitly handling your special values, and deferring to super for all others. Pseudocode:

    getAsString(FacesContext ctx, UIComponent component, Object value) {
        if (value is NaN) {
            return your-own-NaN-string;
        }
    
        if (value is infinity) {
            return your-own-infinity-string;
        }
    
        return super.getAsNumber(ctx, component, value);
    }
    
  3. Register the class as a converter and use it instead of f:convertNumber.
Isaac
  • 16,458
  • 5
  • 57
  • 81
  • Well, I was hoping for a better solution, since now I also have to deal with formatting and setting the correct locale. But I guess it's still worth accepting your answer. :) Thanks. – geca Jan 29 '13 at 15:00
  • If you find a better solution, I'll definitely be happy to know. – Isaac Jan 29 '13 at 18:46