-3

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.

  • What do you mean by `doesn't work` please explain further so we can help you :) – SamHoque Oct 24 '18 at 17:54
  • 2
    Possible duplicate of [How to round a number to n decimal places in Java](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – Nicholas K Oct 24 '18 at 17:55
  • @Rab the output is still too many decimal places and i need it to only be two hope that helps – NoviceProgram12 Oct 24 '18 at 17:57

1 Answers1

2

I am new to programming

So the first thing you need to learn is how to simplify the problem.

I want to limit the output of the result to two decimal place so it looks neater

So, forget about the rest of your application and learn how to do exactly that:

double value =  123.45678;
DecimalFormat decFormat = new DecimalFormat("0.00");
String formatted = decFormat.format(value);
System.out.println( formatted );

I am writing a java gui program which converts different units of measurement.

That is irrelevant to you question. As you can see from the above example you first test a new concept with hardcoded data.

Once you get that working, then you worry about the mathematical calculations to dynamically get the "value" that you want to format.

My output currently is too many decimal places

label.setText(Double.toString(result));
DecimalFormat decFormat = new DecimalFormat("0.00");
decFormat.format(result);

Do you see the problems with the above code?

  1. you set the text of the label BEFORE you format the result
  2. you don't assign the formatted text to a variable so that last statement doesn't do anything.
camickr
  • 308,339
  • 18
  • 152
  • 272
  • I understand the concept as that is not the issue and i understand how the decimal format works, the thing that is confusing is how to get the end result formatted in the way that i would like. – NoviceProgram12 Oct 24 '18 at 18:02
  • This is a very high quality answer +1 – SamHoque Oct 24 '18 at 18:04
  • i have now manged to get it working sorry i was a little confused. Will mark your answer as correct as soon as i can. Thank you and sorry for any frustration caused – NoviceProgram12 Oct 24 '18 at 18:05
  • `i was a little confused` - which is why I keep stressing to simplify the problem. Forget about the application and concentrate on the specific task. Creating a simple example like I provided is how you learn the usage of a new method. – camickr Oct 24 '18 at 18:07