Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By gimbal2

Thread: About BigDecimal

  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default About BigDecimal

    There is a question where I have to compute future investment value using the following method header:

    public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years)
    for which the output is as example follow:

    Years Future value
    1 1093.80
    2 1196.41
    ...
    29 13467.25
    30 14730.57

    I notice that there should be 2 decimal places behind regardless of the decimal places are 0 or not(the zero should be display)
    I have search on Internet and found that BigDecimal is correct way to deal with money. My snippet code as below.

    Java Code:
           for(int i = 1; i <= years; i++){
                System.out.println(i + "\t\t" + futureInvestmentValue(investmentAmount, monthlyInterestRate, i));
            }
    Java Code:
        public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
            double futureInvestmentValue = investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);
            BigDecimal newVal = new BigDecimal(futureInvestmentValue);
            BigDecimal displayVal = newVal.setScale(2, RoundingMode.HALF_DOWN);
            Double d = displayVal.doubleValue();  
                    
            return d;
        }
    The problem i faced here is that BigDecimal cannot be return in double method, and thus I try to convert it to double using
    Double d = displayVal.doubleValue();
    Although it managed to compile, but converting BigDecimal back to double again results in the 0 in decimal places not displayed.

    How should I solve this problem?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,409
    Rep Power
    20

    Default Re: About BigDecimal

    Have the method return a BigDecimal?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: About BigDecimal

    Quote Originally Posted by DarrylBurke View Post
    Have the method return a BigDecimal?

    db
    yes, changing the return value type from double to BigDecimal will solve the problem but at the same time it violates the specifications of the questions which require the use of

    Java Code:
    public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years)
    .

    I wonder is there any alternative by solving the question and at the same time conforming to the specification of the question.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,084
    Rep Power
    6

    Default Re: About BigDecimal

    Its the difference between data and the presentation of data; keep your double as it is, no rounding necessary. But at the time of PRESENTING the number, for example in a System.out.println(), use a number formatter to print the number rounded down to two decimals.

    Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    This way you don't lose any precision and you still get the number displayed in the way you want.
    dojob likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Rubel Rana is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: About BigDecimal

    How you post this code in this format?How you give the line number in your code?I am new here.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,084
    Rep Power
    6

    Default Re: About BigDecimal

    Quote Originally Posted by Rubel Rana View Post
    How you post this code in this format?How you give the line number in your code?I am new here.
    Guide For New Members
    Rubel Rana likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: About BigDecimal

    Quote Originally Posted by gimbal2 View Post
    Its the difference between data and the presentation of data; keep your double as it is, no rounding necessary. But at the time of PRESENTING the number, for example in a System.out.println(), use a number formatter to print the number rounded down to two decimals.

    Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    This way you don't lose any precision and you still get the number displayed in the way you want.
    Thanks. I have compiled the new source code with exactly the same output as displayed based on this.

  8. #8
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: About BigDecimal

    printf("%.3f", decimal) prints 3.21023 as 3.210 without removing the trailing zero

    Is there printf method that print n number of decimal places and does not display trailing zero? eg: 3.21 instead of 3.210
    I have read through the documentation for formatting numeric output but could not find any. Maybe i miss it somewhere.

    Using decimal formatter is good but it will made code to be lengthy.
    Last edited by dojob; 08-02-2013 at 09:06 PM.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,084
    Rep Power
    6

    Default Re: About BigDecimal

    Quote Originally Posted by dojob View Post
    Using decimal formatter is good but it will made code to be lengthy.
    Word of advice: you're a programmer, writing code is what you do. you shouldn't be afraid of or bothered by typing because you're going to be doing that for a large chunk of your life.

    Having a few more lines of code is NOT a problem and thus not an argument to not do something.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: About BigDecimal

    So I presumed that there is no printf method for not displaying trailing zero for specific number of decimals..

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

    Default Re: About BigDecimal

    Use DecimalFormat for that sort of formatting.
    That gives you control over trailing zeroes and the like.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,084
    Rep Power
    6

    Default Re: About BigDecimal

    Quote Originally Posted by Tolls View Post
    Use DecimalFormat for that sort of formatting.
    That gives you control over trailing zeroes and the like.
    I'd tag a like to that, if the article I linked to didn't already spell it out in the same easy English you're using. (disclaimer: Not a snide remark towards you)
    Last edited by gimbal2; 08-05-2013 at 11:50 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,089
    Rep Power
    20

    Default Re: About BigDecimal

    I meant to mention that...
    I must have got bored after testing out a couple of formats.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. BigDecimal
    By tarka in forum New To Java
    Replies: 4
    Last Post: 11-12-2011, 08:22 PM
  2. using the BigDecimal class
    By jopeters in forum New To Java
    Replies: 3
    Last Post: 10-07-2011, 05:56 AM
  3. More sensible BigDecimal toString()?
    By morello in forum Advanced Java
    Replies: 3
    Last Post: 09-29-2011, 04:08 PM
  4. Using BigDecimal
    By doymand in forum New To Java
    Replies: 2
    Last Post: 10-30-2010, 09:25 AM
  5. BigDecimal question
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •