In Java, we can use BigDecimal data type to perform calculation on all monetary values.
To represent and handle monetary values, java.math.BigDecimal class provides :
1) Ability to specify a scale, which represents the number of digits after the decimal place
2) Ability to specify a rounding method
BigDecimal Arithmetic
Here are the common operations on BigDecimals :
The following example demonstrates creating BigDecimals and performing some operations on them.
BigDecimal money1 = new BigDecimal("100.26"); BigDecimal money2 = new BigDecimal("59.75"); BigDecimal result; // Calculate Sum result = money1.add(money2); // Calculate difference result = money1.subtract(money2);
Formatting BigDecimal values in respective Currencies
For formatting monetary values in different currencies, we can use NumberFormat.getCurrencyInstance() helper method.
For example, the below code creates two BigDecimal values, performs some operations on them and show the results in US and Japanese currencies.
package com.topjavatutorial.quiz; import java.math.BigDecimal; import java.text.NumberFormat; import java.util.Locale; public class BigDecimalDemo { public static void main(String[] args) { BigDecimal money1 = new BigDecimal("100.26"); BigDecimal money2 = new BigDecimal("59.75"); BigDecimal result; // Sum and display the result as US dollar result = money1.add(money2); System.out.println("Sum = " + formatMoneyByLocale(result.doubleValue(), Locale.US)); // Calculate difference and display the result as US dollar result = money1.subtract(money2); System.out.println("difference = " + formatMoneyByLocale(result.doubleValue(), Locale.JAPAN)); } private static String formatMoneyByLocale(double value, Locale inLocale) { NumberFormat format = NumberFormat.getCurrencyInstance(inLocale); return format.format(value); } }
Output:
Sum = $160.01
difference = ¥41
The above method formatMoneyByLocale() accepts a double value and performs the formatting by the Locale provided.
Comparing BigDecimal values
We should never use the equals() method to compare BigDecimals as it compares the scale.
For example, this comparison will return false :
BigDecimal money3=new BigDecimal("100.00"); BigDecimal money4 = new BigDecimal("100.0"); System.out.println(money3.equals(money4)); // prints false
The correct way to compare two BigDecimal numbers is with the compareTo() function.
BigDecimal money3=new BigDecimal("100.00"); BigDecimal money4 = new BigDecimal("100.0"); System.out.println(money3.compareTo(money4)); // prints 0
This will print 0.
compareTo() method returns 0 if both numbers are equal, -1 if first number is less than the second one and 1 if first number is greater.
Rounding and Scaling with BigDecimal values
To specify the number of digits after the decimal point, we can use the setScale() method.
We should also specify the rounding mode along with the scale.
The following code confirms the BigDecimal values to always have two decimal places.
BigDecimal value = new BigDecimal("1.234"); value = value.setScale(2, RoundingMode.HALF_UP); System.out.println(value);// prints 1.23
HALF_UP rounds towards “nearest neighbor” unless both neighbors are equidistant, in which case, it rounds up.
Similarly, we can use the following RoundingMode enumeration values :
Here are some more examples of rounding with BigDecimals:
Round up BigDecimal to Integer value
BigDecimal value = new BigDecimal("1.234"); value = value.setScale(0, RoundingMode.UP); System.out.println(value); // prints 2
To provide specific rounding with the BigDecimal class, we can also use a MathContext object as shown in following example.
Round BigDecimal to the nearest whole value
BigDecimal value = new BigDecimal("101.234"); value = value.round(new MathContext(3, RoundingMode.HALF_UP)); System.out.println(value); // prints 101
Note:
The Money and Currency API is currently under development and should be part of Java 9. See this JSP for more info :
https://jcp.org/en/jsr/detail?id=354
You may also like :
Java Coding Interview Questions-Part 3
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post