For some people already familiar with Java might used to hear or experience this event. With slightly experiment, I’ll try to calculate some numeric variable using BigDecimal as java type variable, if we’ll do some process with addition, alleviation, mulitiplication and division by using BigDecimal command method we can use these syntax “add()”, “substract()”, “multiply()” and “divide()” to complete, all these process could worked like expected, but these process could produce some error if we do some process with alleviation using command syntax “divide” which is produce return value with fraction or value with decimal place, and an error view that produce by these process if we used command syntax “printStackTrace” could be like this.
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1514)
…
…
…
…
…
at java.lang.Thread.run(Thread.java:619)
public class SuatuClass {
static BigDecimal bilAwal = BigDecimal.valueOf(10);
static BigDecimal bilAkhir = BigDecimal.valueOf(15);
static BigDecimal hasilBagi = BigDecimal.valueOf(0);
public static void main(String[] args) {
divideOperation();
}
public static void divideOperation() {
hasilBagi = bilAwal.divide(bilAkhir);
System.out.println("Print Hasil Akhir "+ hasilBagi);
}
}
For a better clear view about utility and property of RoundingMode can see on this link: http://java.sun.com/j2se/1.5.0/docs/api/java/math/RoundingMode.html.
If we still want a fraction value behind decimal point than a command line syntax that shows above need some change to fulfill requirement with divide command like shown below “bilAwal.divide(bilAkhir, 3, RoundingMode.HALF_UP);” numeric value which place in the middle of three parameter has a function to determine amount of number behind a decimal point should place on result value, source code shown above if we like to place three digit number as fraction behind decimal point.
For a better clear view about utility and property of BigDecimal can see on this link: http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html.
Perhaps for this moment this knowledge that I can share to all reader, if in this article contain some mistake, please don’t mind to correct me if I done some wrong or give me some advise and comment.
Read More...