Reputation: 4472
I've created a kind of custom type on a JSF project using PrimeFaces. I'd use this type with PrimeFaces's inputNumber but I get the error:
NumberFormatException class java.lang.NumberFormatException java.lang.NumberFormatException at java.math.BigDecimal.(BigDecimal.java:550) at java.math.BigDecimal.(BigDecimal.java:383) at java.math.BigDecimal.(BigDecimal.java:806) at org.primefaces.component.inputnumber.InputNumberRenderer.formatForPlugin(InputNumberRenderer.java:292) at org.primefaces.component.inputnumber.InputNumberRenderer.encodeScript(InputNumberRenderer.java:231) at org.primefaces.component.inputnumber.InputNumberRenderer.encodeEnd(InputNumberRenderer.java:124)
In short, I've created a class MyCurrency
that stores a double
and extends ValueExpression
, like the following:
public final class MyCurrency extends ValueExpression implements Comparable<MyCurrency>, Serializable {
private Double value;
private MyCurrency(final Double value) {
this.value = value;
}
public Double getValue() {
return this.value;
}
public Long longValue() {
return value.longValue();
}
@Override
public int compareTo(final MyCurrency o) {
return this.getValue().compareTo(o.getValue());
}
@Override
public Object getValue(final ELContext context) {
return new BigDecimal(this.value);
}
@Override
public void setValue(final ELContext context, final Object value) {
this.value = new Builder().withValue(value).build().value;
}
public static class Builder {
private Double value;
public Builder withValue(final Double value) {
this.value = value;
return this;
}
public Builder withValue(final Long value) {
this.value = new Double(value);
return this;
}
public Builder withValue(final Object value) {
this.value = Double.parseDouble(value.toString());
return this;
}
public MyCurrency build() {
return new MyCurrency(this.value);
}
}
}
And in my bean I've a property with type MyCurrency
.
When I use it with an inputNumber:
<p:inputNumber id="importoDa" value="#{myBean.myAmount}" />
I get the error [NumberFormatException]
.
Any help, please?
Upvotes: 1
Views: 547
Reputation: 105
Not sure if it's a solution for what you are asking, but it seems that you are trying to format the input of your inputNumber
as currency an compare it's value to another object. It might be easier to store only the double
or BigDecimal
value in your bean and format it in the view as currency. You can achieve this using the symbol
and decimalPlaces
properties of the <p:inputNumber>
tag this way:
<p:inputNumber id="importoDa" value="#{myBean.myAmount}" symbol="$" decimalPlaces="2" />
Hope it helps :)
Upvotes: 3