0

Im using Jibx to write my own binding for my java objects. Below is the block from my binding xml for a bigdecimal field.

....
<structure field="testVO">
<value name="joiningBonus" field="joiningBonus"/>
</structure>
....

On Marshalling im getting the Bigdecimal value with more decimal values. Below is the block of the output xml.

....
<joiningBonus>12345.45639999999912106432020664215087890625</joiningBonus> 
....

If i want Bigdecimal(21,6) precision, how to achieve it??

Balaji Kannan
  • 407
  • 6
  • 24

1 Answers1

0

Thanks none replied, it paved way to solve it by myself,

JiBX has functionality of serilizing and deserializing the values.

Below is part of my binding xml

<format label="decimalFormat" type="java.math.BigDecimal"
            serializer="com.test.Formatter.serializeBigDecimal" />

This is the format specifier tag, Formatter.serializeBigdecimal is the static method in public class which will be called on usage of format like below

<value name="joiningBonus" field="joiningBonus" format="decimalFormat"/>

Below is the public class and static method

public class Formatter{

    public static String serializeBigDecimal(BigDecimal joiningBonus) {
        return joiningBonus.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
    }
}

Below is the before usage of serializer and after its usage output

Before:

......
<joiningBonus>12345.3467570000054754674578454567<joiningBonus>
......

After:

......
<joiningBonus>12345.346757<joiningBonus>
......
Balaji Kannan
  • 407
  • 6
  • 24