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"/>

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.


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).