1

The division of two integers is resulting in a floating point value. Is there a way to get the result as an integer in the following situation?

<p:commandButton value="#{bean.intA / bean.intB}" ../>

The result should be shown as "1" not as "1.0".

sinclair
  • 2,545
  • 4
  • 22
  • 49

1 Answers1

2

EL doesn't support upcasting/formatting. An universal solution would be to create a custom EL function referring the below utility method:

public static String formatNumber(Number number, String pattern) {
    if (number == null) {
        return null;
    }

    DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
    formatter.applyPattern(pattern);
    return formatter.format(number);
}

You can then use it as below:

<p:commandButton value="#{util:formatNumber(bean.intA / bean.intB, '#')}" ... />

In case you happen to use JSF utility library OmniFaces, it's available as #{of:formatNumber()} whose source code is actually copied above.

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513