Monday, October 27, 2008

“Non-terminating decimal expansion” How could this occurred?

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)


Necessary to know if we used some framework in our project, any framework, than content of some dot in error view above will figure printStackTrace message relate on that framework. This thing could occurred in alleviation processed. We can use this calculate process syntax bellow as example source code:

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);
}
}

And now we’ll try focus our attention to a line of this source code “bilAwal.divide(bilAkhir);” if return value from calculation is a fraction than it’ll produce error view like printStackTrace above, and now, how to breaking the ice? “Gampang.. Ketik reg spasi manjur kirim ke……” Hehe.. Just kidding (Indonesian joke style), if we want result value produce by this process is an integer than a line of source code should change into like this “bilAwal.divide(bilAkhir ,RoundingMode.HALF_UP);” property HALF_UP will produce value which rounded up if fraction value behind decimal point is greater than 0.5, for better explanation we can see a table to figure some property and utility in a class RoundingMode below:


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...