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?
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?
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()
)
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));