# Using BigDecimal

• 10-30-2010, 05:39 AM
doymand
Using BigDecimal
I'm trying to make a program that will add the sum of an infinite geometric series(1 + 1/2 + 1/4 + 1/8....)

for (count; count<= limit; count++)
a = ((1)/(math.pow(2,count));
b += a;

This is easy to do when "b" and "a" are doubles, but they do not provide a high enough precision, I want to use BigDecimal to many decimal places, but I am having trouble doing it.

How do you show how many decimal points you want when creating the BigDecimal variable, and then add them together to get an answer?

How would I go about it? I know i'l just get more .9's and know that it's equal to two, but I would still like to know how to do it.
• 10-30-2010, 06:07 AM
Zack
What do you mean how many decimal points you want? BigDecimal goes to infinite precision until your computer runs out of memory.

You could literally have 0.9 followed by a googol 9s if your computer was powerful enough.
• 10-30-2010, 09:25 AM
j2me64
look at the methods of the class BigDecimal here to find out which operations the class offers. and for formatting numbers you can use the class NumberFormat specified and explained here