• 08-02-2013, 05:08 PM
dojob
There is a question where I have to compute future investment value using the following method header:

Quote:

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.

Code:

```      for(int i = 1; i <= years; i++){             System.out.println(i + "\t\t" + futureInvestmentValue(investmentAmount, monthlyInterestRate, i));         }```
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?
• 08-02-2013, 05:21 PM
DarrylBurke
Have the method return a BigDecimal?

db
• 08-02-2013, 05:35 PM
dojob
Quote:

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

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.
• 08-02-2013, 05:49 PM
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.
• 08-02-2013, 05:51 PM
Rubel Rana
How you post this code in this format?How you give the line number in your code?I am new here.
• 08-02-2013, 05:56 PM
gimbal2
Quote:

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
• 08-02-2013, 07:01 PM
dojob
Quote:

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.
• 08-02-2013, 09:03 PM
dojob
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.
• 08-02-2013, 10:10 PM
gimbal2
Quote:

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.
• 08-03-2013, 05:43 AM
dojob
So I presumed that there is no printf method for not displaying trailing zero for specific number of decimals..
• 08-05-2013, 11:33 AM
Tolls
Use DecimalFormat for that sort of formatting.
That gives you control over trailing zeroes and the like.
• 08-05-2013, 11:48 AM
gimbal2