Reputation: 12393
I have an inputText that takes a value from the user and is bound to a double. If the value is "5000", subsequent pages will show it as "5000.0". Is there any way to format it such that if the user enters a non-fractional number, it won't show the decimal?
Examples:
User enters "5000"
Show "5000"
user enters "5000.1"
Show "5000.1"
<h:inputText value="#{sessionScope.eventDO.area}" id="areaInTxtId">
<f:convertNumber pattern="###0"/>
</h:inputText>
I've tried using the f:convertNumber tag below but I get this error:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
which I don't want to change any of the Java code. Only the presentation of the double.
Upvotes: 0
Views: 1133
Reputation: 30025
If you don't find a pattern you could write your own extension of NumberConverter
. I never tried it, but it should work similiar as this:
@FacesConverter(value="ExampleConverter")
public class ExampleConverter extends NumberConverter {
String getAsString(FacesContext context, UIComponent component,Object value) {
// call super.getAsString(...) and cut trailing .0
}
}
Use the converter in the facelet this way:
<h:inputText value="#{sessionScope.eventDO.area}" id="areaInTxtId">
<f:converter converterId="exampleConverter" />
</h:inputText>
Upvotes: 1
Reputation: 963
Converter will just convert the number for further usage. If you want to add specific behaviour while interacting with user, try and use javascript "onkeyup" event and manipulate with the data supplied. Presentation is user-specific and thus should be outside the server.
Upvotes: 0