user357032
user357032

Reputation: 115

Multiplying doubles in java?

I'm trying to multiply the same double (square it) but the number comes out wrong. When i display the double by itself the number is correct but when i multiply it by itself it comes up with the wrong number. I already tried using the math.pow function and got the same result.

    Double height=Double.parseDouble(myPrefs.getString("Heightent",""))*.0254;
    Double bmi = (height*height);
    dbmi.setText(bmi.toString());

Height is entered in a different activity in inches. When i display height in a text box it comes out to be the right number. For example, Heightent entered is 74 and 1.8796 is displayed when i put height in a textview. Butwhen I use the code above the number 16 is displayed in TextView dbmi. Any help?

Upvotes: 4

Views: 6348

Answers (2)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

I tried both

    Double height=Double.parseDouble("74")*.0254;
    Double bmi = (height*height);
    System.out.println("first " + bmi);


      height=Double.parseDouble("74");
      double temp = height.doubleValue()*.0254;
      bmi = (temp*temp);
      System.out.println("Second " + bmi);

and on Calc

and got same every where..... enter image description here

Upvotes: 1

Deepend
Deepend

Reputation: 4155

Check out this great post by Stephen C.

https://stackoverflow.com/a/5385202/1214163

It should provide everything you need. He suggests that if you need to work with big numbers that you use the BigDecimal class

Upvotes: 3

Related Questions