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

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

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.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}