Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Using BigDecimal for Mathematical Formulas

1. ## Using BigDecimal for Mathematical Formulas

I am simply confused on how to convert the equation below for bigDecimal objects. I have already tried this, and this and the output is really weird once I call the method.
The first block of code is what I'm trying to convert into BigDecimal arithmetic.

public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months)
{
double futureValue = 0;
for (int i = 1; i <= months; i++)
{
futureValue =
(futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
// System.out.println("Month: " + i + " FutureValue: " + futureValue);
}
return futureValue;
}
}

My attempt at this is as follows:

public static BigDecimal calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months)
{
BigDecimal futureValue = new BigDecimal(0.0);
BigDecimal montlyInvestmentDecimal = new BigDecimal(monthlyInvestment);
BigDecimal montlyInterestRateDecimal = new BigDecimal(monthlyInterestRate);
BigDecimal one = new BigDecimal(1.0);
for (int i = 1; i <= months; i++)
{
System.out.println("Month: " + i + " FutureValue: " + futureValue);
}
return futureValue;
}

Output:
Welcome to the Future Value Calculator

DATA ENTRY
Enter monthly investment: 1
Enter yearly interest rate: .01
Enter number of years: 3
Month: 1 FutureValue: 0E-66
Month: 2 FutureValue: 0E-132
Month: 3 FutureValue: 0E-198
Month: 4 FutureValue: 0E-264
Month: 5 FutureValue: 0E-330
Month: 6 FutureValue: 0E-396
Month: 7 FutureValue: 0E-462
Month: 8 FutureValue: 0E-528
Month: 9 FutureValue: 0E-594
Month: 10 FutureValue: 0E-660
Month: 11 FutureValue: 0E-726
Month: 12 FutureValue: 0E-792
Month: 13 FutureValue: 0E-858
Month: 14 FutureValue: 0E-924
Month: 15 FutureValue: 0E-990
Month: 16 FutureValue: 0E-1056
Month: 17 FutureValue: 0E-1122
Month: 18 FutureValue: 0E-1188
Month: 19 FutureValue: 0E-1254
Month: 20 FutureValue: 0E-1320
Month: 21 FutureValue: 0E-1386
Month: 22 FutureValue: 0E-1452
Month: 23 FutureValue: 0E-1518
Month: 24 FutureValue: 0E-1584
Month: 25 FutureValue: 0E-1650
Month: 26 FutureValue: 0E-1716
Month: 27 FutureValue: 0E-1782
Month: 28 FutureValue: 0E-1848
Month: 29 FutureValue: 0E-1914
Month: 30 FutureValue: 0E-1980
Month: 31 FutureValue: 0E-2046
Month: 32 FutureValue: 0E-2112
Month: 33 FutureValue: 0E-2178
Month: 34 FutureValue: 0E-2244
Month: 35 FutureValue: 0E-2310
Month: 36 FutureValue: 0E-2376

FORMATTED RESULTS
Monthly investment: \$1.00
Yearly interest rate: 0.0%
Number of years: 3
Future value: 0E-2376

Continue? (y/n):

So clearly this still isn't working. Can anybody help?

2. ## Re: Using BigDecimal for Mathematical Formulas

0E-66 means that you have a decimal places zero of 66 digits. like 12.666666666666666666666... (66 digits of decimal digits) whose value when multiply by zero is 0 (any number multiply by zero is zero).
example big decimal value of 3.200000000000000177635683940025046467781066894531 25 multiply by zero will result of 0E-50 means 0.0000000000000000000000000000000000000000