Question

I have an h:inputText control where I can type in numbers up to 7 digits and it will convert them to a decimal representation (ie. type in "9999999" and it will render "9999999.0"). However, when I type in any number with 7 digits or more, it will convert it to scientific notation (ie. type in "10000000" and it will render it as "1.0E7").

As a business requirement, I must display it in decimal representation and not scientific notation. Does anyone have a way to do this?

<h:inputText id="tableQuantityId" 
        value="#{fee.tableQuantity}" 
        disabled="#{!fee.selected}" 
        rendered="#{editable}" 
        validator="#{facesValidator.validateQuantity}">
    <a4j:support event="onchange" reRender="messages, feePart" ajaxSingle="true"/>
</h:inputText>

Edit: After some further investigation, it seems that it's getting it's current format from the type "double". (In other words, you can assign "10000000" to a double and print it and it will show it back to you in scientific notation).

So I went into my getTableQuantity() method and changed it from:

(double version)

public double getTableQuantity() {
    return tableQuantity;
}

(to String representation):

public String getTableQuantityFormatted() {

    double d = tableQuantity;
    NumberFormat formatter = new DecimalFormat("###.#####");

    String f = formatter.format(d);
    return f;
}

and I changed "value="#{fee.tableQuantity}" to value="#{fee.tableQuantityFormatted}" in my xhtml

But now I get the following error on the xhtml page:

The quantity value 10000000 is incorrect. /page/feeContent.xhtml @70,58 value="#{fee.tableQuantityFormatted}": Property 'tableQuantityFormatted' not writable on type java.lang.String

Was it helpful?

Solution

<h:inputText 
id="xy"
value="10000000">
  <f:convertNumber maxIntegerDigits="10" maxFractionDigits="1" pattern="#########0.0"/>
</h:inputText>

The tag f belongs to:

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

in my case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top