I am writing a java gui program which converts different units of measurement. I want to limit the output of the result to two decimal place so it looks neater however i have been struggling to get it to work. Below is my code please can someone help.
if (text.isEmpty() == false) {
double value = Double.parseDouble(text);
// the factor applied during the conversion
double factor = 0;
// the offset applied during the conversion.
double offset = 0;
// Setup the correct factor/offset values depending on required conversion
switch (combo.getSelectedIndex()) {
case 0: // inches/cm
factor = 2.54;
break;
case 1: // miles/km
factor = 1.60;
break;
case 2: // pounds/kilograms
factor = 0.45;
break;
case 3: // gallons/Litres
factor = 4.54;
break;
case 4: // feet/meters
factor = 0.30;
break;
case 5: // celsius/kelvin
factor = 1;
offset=273.15;
break;
case 6: // acres/hectare
factor = 2.471;
break;
}
double result = 0;
if(reverseCheck.isSelected() == true) {
result = factor / value - offset;
}else {
result = factor * value + offset;
}
count++;
labelCount.setText("Conversion Count: "+count);
label.setText(Double.toString(result));
DecimalFormat decFormat = new DecimalFormat("0.00");
decFormat.format(result);
I am new to programming so if you could please explain why this code isn't functional then that would be much appreciated. My output currently is too many decimal places and i need it to only be 2 decimal places.