0

In this line:

totalCost += df.format(pCost);

I get the error 'inconvertable types' required: double, found: string, yet the pCost variable is a double, and the method that made it outputs it as a double.. does anyone know why this may happen?

Chillo
  • 87
  • 2
  • 7

2 Answers2

2

use just this:

totalCost += pCost;

or, if you will:

totalCost += Double.valueOf(df.format(pCost));

see: Round a double to 2 decimal places

edit: as a response to the caveat below: This code isn't truely precise; follow the link for a more accurate version (in short: convert it to a BigDecimal and use .setScale())

Community
  • 1
  • 1
ljgw
  • 2,751
  • 1
  • 20
  • 39
1

Well Decimal format really does return String:

DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(34.4959);

but to get a Double you could do:

Double.valueOf(df.format(34.4959));
Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63