Java BigDecimal Examples

Java has the BigDecimal class to represent currency.A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001) could result in the 0.001 being dropped alltogether when summing as the difference in magnitude is so large. With Java BigDecimal this would not happen.If you are dealing with money, precision is a must, use BigDecimal class.

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.The BigDecimal class gives its user complete control over rounding behavior.

The disadvantage of Java BigDecimal is that it’s slower.

I hopeĀ above example will be really useful when you are dealing money/currency related calculations.

About mohan

This is Mohan Jayapalan author of TechPages Blog.I am a part time blogger and Currently working for private Software Concern.

© Copyright 2010-2016 · All Rights Reserved · Powered by WordPress ·