Search code examples
javaxmlbean-io

How to format beanio in Java?


Wasn't sure how to better describe the problem in the title, but basically I have a beanio XML where one of the fields is this:

<field name="number" length="19" padding="0" justify="right"/>

My issue is that the value that goes here can be negative or positive, but I need the positive or negative sign to be at the front while the number itself is in the back.

For example, I am currently getting:

0000000000000-10500

However, what I need is:

-000000000000010500 

Is there a way to edit the field so that it comes out with the negative/positive sign in front?


Solution

  • Disclaimer: This is not tested

    You can try:

    <field name="number" length="19" padding="0" justify="right" format="-#0"/>
    

    OR

    <field name="number" length="19" padding="0" justify="right" format="#0;-#0"/>
    

    OR if that fails:

    <field name="number" length="19" padding="0" justify="right" format="##################0;-##################0"/>
    

    18 x '#' and 1 x '0'

    OR

    <field name="number" length="19" padding="0" justify="right" format="0000000000000000000;-0000000000000000000"/>
    

    19 x '0'.

    See the Reference Guide and just google how to use the decimal format pattern for the java.lang.Number field - DecimalFormat API