I focusing on different web-services, and that i always employ WSDL First.

JAXB creates for any Type like:

<xsd:simpleType name="CurrencyFormatTyp">
    <xsd:restriction base="xsd:decimal">
        <xsd:totalDigits value="13"/>
        <xsd:fractionDigits value="2"/>
        <xsd:minInclusive value="0.01"/>
    </xsd:restriction>
</xsd:simpleType>

a Java binding type BigDecimal (as it is pointed out in JAXB specs).

After I then do a little arithmetic operation with values from the type double (that are saved inside a database and planned via hibernate towards the type double) I encounter trouble.

<ns5:charge>0.200000000000000011102230246251565404236316680908203125</ns5:charge>        
<ns5:addcharge>0.0360000000000000042188474935755948536098003387451171875</ns5:addcharge>
<ns5:tax>0.047199999999999998900879205621095024980604648590087890625</ns5:tax>
<ns5:totalextax>0.2360000000000000153210777398271602578461170196533203125</ns5:totalextax>

What will be the proper way?

  1. Convert my values into double (JAXB binding from BigDecimal to double)
  2. Hibernate mapping double to Bigdecimal

and do my arithmetic procedures in a single object type.

  • Browse the Floating-Point Guide
  • Never use double or float for the money amounts
  • Use BigDecimal rather, that is how it is for

You do not ever desire to use floating point formats (for example double and float in Java) for currency procedures, simply because they have limited precision and also have been made to hold values which are in some way based on calculating (by which situation they are not absolutely precise to start with, by which situation the limited precision is a lesser problem).

What Every Computer Scientist Should Know About Floating-Point Arithmetic is the article around the subject. It's a little heavy on math, however it helps you to realize that (Alternatively, the content associated with by Michael Borgwardt is much simpler to know but still demonstrates/describes the issue).

To avert this type of problems, make certain that you employ BigDecimal solely inside your code and that exterior storage/transfer points use fixed-point/arbitrary-precision values too (i.e. your database should not store floating point amounts either).