VORiAND
VORiAND

Reputation: 181

Using p:inputNumber with BigDecimal and comma separator

I want to use with "," as a separator, regarding my locale settings. My main problem is, that the number(s) after the separator doesn't even get to the bean itself.

I already tried to add decimalSeparator="," and to the but nothing changed...

This cellEditor includes the editor and the output:

<p:cellEditor>
                <f:facet name="output">
                    <h:outputText id="grossMoney" value="#{item.grossMoney}">
                        <f:convertNumber locale="hu"/>
                        <f:convertNumber maxFractionDigits="2" minFractionDigits="0"/>
                    </h:outputText>                        
                </f:facet>
                <f:facet name="input">                         
                    <p:inputNumber id="grossMoneyEdit" decimalSeparator="," value="#{item.grossMoney}" >
                        <f:convertNumber locale="hu"/>
                        <!-- <f:convertNumber maxFractionDigits="2" minFractionDigits="0"/> -->                        

                    </p:inputNumber>
                </f:facet>
</p:cellEditor>

This is the bean method, where the setter doesn't get the value right:

public void setGrossMoney(BigDecimal grossMoney) {
    if (grossMoney != null && grossMoney != BigDecimal.ZERO) {

        if (vatRate == 0) {
            netUnitPrice = grossMoney;
        } else {
            netUnitPrice = grossMoney.divide(BigDecimal.ONE.add(BigDecimal.valueOf(vatRate).divide(BigDecimal.valueOf(100L))), 3, RoundingMode.HALF_UP);
        }

    } else {
        netUnitPrice = BigDecimal.ZERO;
    }
}

Expected result is simple: if I enter 100,5, it should be converted to BigDecimal and calculate with the correct value and on the output side it should be presented as 100,5 as well.

Upvotes: 0

Views: 1382

Answers (1)

Selaron
Selaron

Reputation: 6184

You are somehow specifying the same aspect in multiple ways: converter, decimalSeparator, language, multiple converters in a single component.

Primefaces p:inputNumber has a simple language attribute - did you try that?

<p:inputNumber value="#{myBean.decimalVal}" lang="hu"/>
<h:outputText value="#{myBean.decimalVal}">
   <f:convertNumber locale="hu" />
</h:outputText>

You are also not setting the grossMoney field in your setter. Instead you do calculations of netUnitPrice. You should better do that in an action or (action/ajax)listener. Keep getters/setters trivial.

Upvotes: 1

Related Questions