Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Money – Java Monetary Computations

DZone's Guide to

Java Money – Java Monetary Computations

Amit Kumar Mondal provides insight into handling monetary functionality within the Java programming language.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

If you ever tried to build any application which deals with monetary computations, you might have written a responsible class dealing with such hefty operations using BigDecimal.

Float/Double Arithmetic

If you try to develop your monetary computation class using float or double arithmetic you would encounter unexpected results which would make you believe that the operations are broken.

public final class FloatingPointProblem {
public static void main(final String[] args) {
System.out.println(0.51 - .4);
}
}

If you execute the aforementioned snippet, it will get the an absurd result - 0.10999999999999999

Basically in Java, we can not represent the actual base 10 numbers and that’s why such problems occur.

Old School Solution

To overcome such challenges, the BigDecimal class has been introduced which proved its convenience over the floating point arithmetic.

What’s the problem?

The problem lies in writing too much boiler-plate code which makes it hard to manage.

JSR 354

This is a Java Specification Request to add new features relating to Monetary computations. It facilitates developer to write manageable codes.

Reading Material

I have found an extensive guide written by the one of the developers of Java Money. It covers all the nitty gritty details of Java Money API.

Java Money

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
java ,money ,programming

Published at DZone with permission of Amit Kumar Mondal, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}