4

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?

user357032
  • 115
  • 1
  • 2
  • 10

2 Answers2

3

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

Community
  • 1
  • 1
Deepend
  • 4,057
  • 17
  • 60
  • 101
  • BigDecimal is a great class. I feel its harder to do multiplication with it though but that might come from my inexperience with the class. By putting Double.parseDouble(myPrefs.getString("Heightent",""))*.0254 in parentheses it fixed the problem. I'm going to accept your answer though because BigDecimal is a great class to use and be familiar with. – user357032 Jun 15 '12 at 12:48
1

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

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36