I have below values, used as testdata to perform subtraction.
order total = $17.99
item Sub Total = $17.99
discount = 20
String orderTotal = "17.99"
String itemSubTotal = "17.99"
String discountPercent = "20"
double discountPercent = Double.parseDouble(discount);
// calculate the discount if given in percentage
double discountAmount = (Double.parseDouble(itemSubTotal) * discountPercent) / 100;
// round of the amount
double discountAmount = round(discountAmount, 2);
Double finalOrderTotal = (Double.parseDouble(orderTotal)) - discountAmount;
System.out.println("---------------------"+finalOrderTotal);
And this is method for rounding off the values :
public static double round(double value, int numberOfDigitsAfterDecimalPoint)
{
BigDecimal bigDecimal = new BigDecimal(value);
bigDecimal = bigDecimal.setScale(numberOfDigitsAfterDecimalPoint,
BigDecimal.ROUND_HALF_UP);
return bigDecimal.doubleValue();
}
So manual calculation expected result would be 17.99 - 3.60 = 14.39
but what I'm getting is 14.389999999999999
I've tried BigDecimal
as suggested in same type of question. but for that again i have to do round of the finalOrderTotal
. isn't it ?
Can someone help me to get the simple code (using BigDecimal
) as i'm doing calculation so i can get the correct result.