# Thread: About BigDecimal

1. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## 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. ## Re: About BigDecimal

Have the method return a BigDecimal?

db

3. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: About BigDecimal

Originally Posted by DarrylBurke
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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

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

5. Member
Join Date
Jul 2013
Posts
14
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: About BigDecimal

Originally Posted by Rubel Rana
How you post this code in this format?How you give the line number in your code?I am new here.
http://www.java-forums.org/forum-gui...w-members.html

7. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: About BigDecimal

Originally Posted by gimbal2
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. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: About BigDecimal

Originally Posted by dojob
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.

10. Member
Join Date
Jul 2013
Posts
52
Rep Power
0

## Re: About BigDecimal

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

11. Moderator
Join Date
Apr 2009
Posts
13,216
Rep Power
23

## Re: About BigDecimal

Use DecimalFormat for that sort of formatting.
That gives you control over trailing zeroes and the like.

12. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: About BigDecimal

Originally Posted by Tolls
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.

13. Moderator
Join Date
Apr 2009
Posts
13,216
Rep Power
23

## Re: About BigDecimal

I meant to mention that...
I must have got bored after testing out a couple of formats.

#### Posting Permissions

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