简体   繁体   中英

Decimal format not limiting values to two decimal places

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM