0

I created a method in a different class that calculates the amount of money the user would have after investing. I am trying to have the method return the value in standard format rather than in scientific notation. I've already tried looking up other answers of the same topic and this is what I got so far

        DecimalFormat df = new DecimalFormat("#.##");      
        df.setMaximumFractionDigits(0);
        endofyearamount = Double.valueOf(df.format(endofyearamount));
        return endofyearamount; 

However, when I run my main method I still get a value of

    1.2902498E7

This is the code in my main method to display the return value in case you need to see it too.

    double answer = formula.calculate(age,  retiredage,  monthlyinvestment,  interestrate,  investedperyear,  endofyearamount, x, y );
        System.out.println(answer);
  • 1
    Is this toy code and your real question is how to format doubles? Or are you actually trying to use doubles to handle money values in the real world? If it is the former, then you need to format() your output. If it is the latter, then avoid using floating point math with money values. – Matt Coubrough Jul 06 '14 at 23:12
  • possible duplicate of [What is the best data type to use for money in java app?](http://stackoverflow.com/questions/8148684/what-is-the-best-data-type-to-use-for-money-in-java-app) – Raedwald Jul 06 '14 at 23:14
  • 1
    Possible duplicate of http://stackoverflow.com/questions/16098046/how-to-print-double-value-without-scientific-notation-using-java – Raedwald Jul 06 '14 at 23:19
  • I didn't ask what type to use when displaying currency. – Cherry_Developer Jul 06 '14 at 23:19

4 Answers4

1

Never use double but rather use BigDecimal when representing money in Java. Floating points cannot be used to represent precise real numbers accurately.

See my response comment on another SO question.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 2
    BigDecimal is not particularly good at precise real numbers in general, or even rationals - consider dividing 1 by 3. It is very, very good at representing finite length decimal fractions, which is indeed useful for money. – Patricia Shanahan Jul 06 '14 at 23:14
1

First of all you shouldn't rely on floating numbers to store money informations, since they don't behave like you'd expect in all situations.

Second thing is: the scientific representation is just a way to write a value. A value can't be in scientific or not scientific representation since it's just a value. What I mean is that there is no way to convert a double into a non specific representation but just a way to print its value in a non scientific way.

The value a double has it's the same regardless how you'd like to print it. You can print it by ignoring some decimal values (and get the corresponding rounding), but in your case you are just doing what? Converting it into a string to retrieve a new value to introduce new errors?

Jack
  • 131,802
  • 30
  • 241
  • 343
1

I assume endofyearamount is a double, which means you are returning a floating point number not a string. No amount of fiddling with the double before turning it will result in changing the formatting done by the System.out.println(answer) call. Instead use

System.out.println(String.format("%.2f", answer));
Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49
1

You don't want to use float of double numbers for calculations involving money.

One simple approach is to use cents as units, not dollars, and store money amounts (as cents) in ints (or longs for large sums).

As an example:

public String centsToString(int cents) {
  int dollars = cents / 100;
  cents = cents - (dollars * 100);
  if (cents < 10)  return "$" + dollars + "." + cents + "0";
  return "$" + dollars + "." + cents;
}

testing with:

System.out.println(new TestAutoTagger().centsToString(13));
System.out.println(new TestAutoTagger().centsToString(99));
System.out.println(new TestAutoTagger().centsToString(100));
System.out.println(new TestAutoTagger().centsToString(101));
System.out.println(new TestAutoTagger().centsToString(999901));

produces:

$0.13

$0.99

$1.00

$1.10

$9999.10