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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Using BigDecimal for Mathematical Formulas

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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++)
    {
    futureValue = (futureValue.add(futureValue)).multiply
    (one.add(montlyInterestRateDecimal));
    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. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default 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

    futureValue = (futureValue.add(futureValue)).multiply
    (one.add(montlyInterestRateDecimal));


    your futureValue is zero and any number multiply by zero is zero. you even have futureValue.add(futureValue), that would be 0 + 0, still 0.

Similar Threads

  1. Mathematical problem with Money amounts
    By craigjlner in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 19th, 2013, 03:12 AM
  2. [SOLVED] working with BigDecimal and MathContext
    By juanp_1982 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: February 13th, 2012, 12:30 AM
  3. Break down BigDecimal to individual values
    By tarkal in forum Java Theory & Questions
    Replies: 17
    Last Post: November 16th, 2011, 09:43 AM
  4. Formulas within my code aren't cooperating
    By bohrstein7 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: September 2nd, 2011, 03:30 PM
  5. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 07:35 AM

Tags for this Thread