-3

After some awesome help yesterday on this site, I am back with another question. I have my input/output equation the way I want it but need help rounding the decimal off to the hundredths position. The way it is set up now the output of pounds seems to just repeat the answer. I have been inputting 10.2 as the weight in kilograms and the answer I keep receiving in pounds is 22.4422.44. I know it is something with the String.format I am using but after messing with it for quite a while I can't seem to figure it out. I know it is something silly but I have been working on this for a while now and I think my brain may be mush. Below is my program.

//This program converts kilograms to pounds using input/output dialog boxes.

import javax.swing.*;

public class SNHU2_3 {

    public static void main (String[] args){

        String inputStr;
        String outputStr;


        double pounds;
        double kilograms;


        inputStr = JOptionPane.showInputDialog("Enter weight in kilograms");
        kilograms = Double.parseDouble(inputStr);

        outputStr = ("Kilograms = " + kilograms) + "\n" + ("Pounds = " +  (kilograms * 2.2)) + String.format("%.2f", (kilograms * 2.2));

        JOptionPane.showMessageDialog(null, outputStr, "Weight Conversion", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);


    }
}
roeygol
  • 4,908
  • 9
  • 51
  • 88
Ph1shPhryd
  • 109
  • 7
  • [This](http://stackoverflow.com/questions/11701399/round-up-to-2-decimal-places-in-java) and [this](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) and [this](http://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places)... did you search? – user1803551 May 21 '15 at 13:20
  • 2
    Not your immediate problem but there are **not** exactly 2.2 pounds in a kilogram. The precise number is 2.20462262185. That will cause you trouble: even with rounding. – Bathsheba May 21 '15 at 13:22

2 Answers2

0

I suggest

    outputStr = String.format("Kilograms = %.2f \n Pounds = %.2f", kilograms, (kilograms * 2.2));

I would first of all suggest that you should thoroughly research your question before posting it. There are many related question that are already asked in this forum. Also study a little bit more about String.format.

Next to explain your problem:

    outputStr = ("Kilograms = " + kilograms) + "\n" + ("Pounds = " +  (kilograms * 2.2)) + String.format("%.2f", (kilograms * 2.2));

What this statement does is it concatenates different strings that you have placed between '+' signs. So we start from the left where your have the String "Kilograms = " so first it is taken then we have kilogram this is basically a double and in your example stores the value 10.2. Now the String becomes "Kilograms = 10.2". After that you have the string "\n". So the string becomes "Kilograms = 10.2\n". Then you have "Pounds = ", so on concatenation the string becomes "Kilograms = 10.2\nPounds = ". After that it hits the expression (kilograms * 2.2) Which in your case is 10.2*2.2 which computes to 22.44 So after concatenation the string becomes "Kilograms = 10.2\nPounds = 22.44". After that it hits String.format("%.2f", (kilograms * 2.2)). The return value of this expression in your case is '"22.44"'. So now the finally concatenated string is "Kilograms = 10.2\nPounds = 22.4422.44" which is finally stored in outputStr. And this is exactly what you get.

I believe that I have been able to explain you what is the problem. SO REMEMBER - you should thoroughly research your question before posting it.

Blip
  • 3,061
  • 5
  • 22
  • 50
0

You can just simply use:

kilograms = Double.parseDouble(inputStr);
Math.round(kilograms);
roeygol
  • 4,908
  • 9
  • 51
  • 88