Is there a way to format a number with fixed length adding separator using Anotation? I ALWAYS receive 10 integer positions and 2 decimal, getting a fixed length of 12
The field anotattion I use:
@Field(at=576, length=12, format="###.##")
private BigDecimal impTotal;
There's an example:
Received: 00000000000150
Value expected: 1.50
Im using this to solve it but I think it could slow the proccess:
public BigDecimal getImpTotal() {
return impTotal.divide(new BigDecimal(100));
}
As you have noticed, the pattern that you supply doesn't coerce the string into what you expect. You can do it the way you currently do it, but when you have to do the inverse and output the value in the fixed length format, you will then have to multiply by 100 to get the desired output.
It is the best to use a TypeHandler implementation to do all of this for you. I can't comment on the performance of your implementation versus that of a type handler, you will need to benchmark it.
You need to implement the
org.beanio.types.TypeHandlerinterface. See the JavaDoc comments for further explanationsYou must then register your type handler with the
StreamBuilderAnd then reference this type handler on the field where you want it to be used:
You can also look at extending the existing
org.beanio.types.BigDecimalTypeHandler, it might not be as simple as your own implementation though.