This might be a duplicate, but I cannot find any answers that work with my code.
I'm trying to truncate my results for a method(for calculating a fee) in Java. I then try to write the results to a text file, but it's not showing like it should. This is what I get and what I'd like it to return:
- Result of fee is 8.4, it should return 8.40
- Result of fee is 8.0, it should return 8.00
And so on... Any suggestions please?
This is my entire code for the method:
public Double calculateFee(Parcel pcl) {
// Get type of parcel (E, S or X)
String typeOfParcel = pcl.getParcelID().substring(0,1);
// Formula for calculating fee
Double fee = (double) 1 + Math.floor(pcl.getVolume()/28000) + (pcl.getDays()-1);
// apply a discount to parcels of type "S"
if (typeOfParcel.equalsIgnoreCase("S")) {
fee = fee * 0.9;
}
// apply a discount to parcels of type "X"
else if (typeOfParcel.equalsIgnoreCase("X")) {
fee = fee * 0.8;
}
// This is what I tried:
// Tried also using #.##, but no result
DecimalFormat decim = new DecimalFormat("0.00");
fee = Double.parseDouble(decim.format(fee));
return fee;
}