formatting - Truncate double or add extra 0 to the decimal - Java -
this might duplicate, cannot find answers work code.
i'm trying truncate results method(for calculating fee) in java. seek write results text file, it's not showing should. , i'd return:
result of fee 8.4, should homecoming 8.40 result of fee 8.0, should homecoming 8.00and on... suggestions please?
this entire code method:
public double calculatefee(parcel pcl) { // type of parcel (e, s or x) string typeofparcel = pcl.getparcelid().substring(0,1); // formula calculating fee double fee = (double) 1 + math.floor(pcl.getvolume()/28000) + (pcl.getdays()-1); // apply discount parcels of type "s" if (typeofparcel.equalsignorecase("s")) { fee = fee * 0.9; } // apply discount parcels of type "x" else if (typeofparcel.equalsignorecase("x")) { fee = fee * 0.8; } // tried: // tried using #.##, no result decimalformat decim = new decimalformat("0.00"); fee = double.parsedouble(decim.format(fee)); homecoming fee; }
the problem here isn't formatting wrong. formatting double using:
decim.format(fee);
then, parse string, double, hence losing formatting:
double.parsedouble(...
just homecoming string rather double, , don't utilize double.parsedouble.
java formatting double truncate
No comments:
Post a Comment