2

Whats the proper/best way to use pe:inputNumber for exact big decimals? I have to accept currency input which can have up to 4 decimals, and must not be rounded. Right now I am doing the following:

public void withdraw(Number amount) {
 BigDecimal d = new BigDecimal(amount.doubleValue());
 // process 
}

and

<pe:inputNumber roundMethod="S" decimalPlaces="4" symbol="#{applicationBean.currentCurrencySymbol}" required="true" value="#{flowScope.Amount}"/>

<p:commandButton value="Proceed" type="submit" ajax="true" actionListener="#{myBean.withdraw(flowScope.Amount)}"
</p:commandButton>

My concern is that value gets converted to Double internally by InputNumber which can potentially lead to rounding errors when I convert it into BigDecimal.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
rootkit
  • 2,165
  • 2
  • 29
  • 43

1 Answers1

1

The PrimeFaces Extensions InputNumber has been moved to PrimeFaces a while ago. The source of the renderer is open. There you'll find that submitted values are directly used to instantiate a BigDecimal:

BigDecimal value = new BigDecimal(submittedValue);

See also the showcase for an example with an example with a BigDecimal: https://www.primefaces.org/showcase/ui/input/inputNumber.xhtml

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102