2013-09-29 118 views
0

我有編碼這個簡單的應用程序做一個用戶input.This單獨計算就是計算被傳遞到兩個變量我的計算類:轉換float結果爲double類型

public void onClick(View v) { 
     // TODO Auto-generated method stub 

     try { 

      String getoffsetlength = offsetLength.getText().toString(); 
      String getoffsetdepth = offsetDepth.getText().toString(); 
      String getductdepth = ductDepth.getText().toString(); 

      double tri1,tri2; 
      double marking1,marking2; 

      double off1 = Double.parseDouble(getoffsetlength); 
      double off2 = Double.parseDouble(getoffsetdepth); 
      double off3 = Double.parseDouble(getductdepth) 
        ; 
      marking1 = Math.pow(off1,2) + Math.pow(off2,2); 
      tri1 = (float)off2/(float)off1; 
      tri2 = (float)off3/Math.atan((float)tri1); 
      marking2 = (float)off3/Math.atan(tri2); 


      Intent myIntent = new Intent(MainActivity.this, CalcResult.class); 
      myIntent.putExtra("number1", marking1); 
      myIntent.putExtra("number2", marking2); 
      startActivity(myIntent); 


     } catch (NumberFormatException e) { 
      // TODO: handle exception 
      System.out.println("Must enter a numeric value!"); 

     } 

    } 

在我的計算結果類我將結果轉換爲文本框中表示的結果的兩倍。我想知道是另一種轉換結果的方式,因爲它們似乎是浮點數或者計算結果是關閉的。

 Intent intent = getIntent(); 
     double mark1 = intent.getDoubleExtra("number1", 0); 
     double mark2 = intent.getDoubleExtra("number2", 0); 

     //set the variables on EditTexts like this : 

     result1 = (EditText)findViewById(R.id.mark1); 
     result2 = (EditText)findViewById(R.id.mark2); 
     result1.setText(mark1+""); 
     result2.setText(mark2+""); 

所得的計算不是我所期待的:

1.什麼我輸入:

https://plus.google.com/112628117356947034778/posts/En1Bueexoc7

2.Resulting計算:

https://plus.google.com/112628117356947034778/posts/ApqinYrp8Na

+0

我絕對不知道你在問什麼。請以您期望的內容,得到的內容以及用於返回您不期望的結果的計算爲例來簡化此問題。所有其餘的代碼都是噪聲。 – Simon

+0

你期望得到什麼?你可以使用轉換來轉換:double dbl =(double)theFloatVarHere; – NormR

+0

@NormR當然,將一個double賦給一個float是沒有意義的? – Simon

回答

1

您正在投射一個LL你的雙打成花車,當您使用(float)計算:

 tri1 = (float)off2/(float)off1; 
     tri2 = (float)off3/Math.atan((float)tri1); 
     marking2 = (float)off3/Math.atan(tri2); 

刪除所有這些從你的代碼,那麼你只曾經處理雙打。

 tri1 = off2/off1; 
     tri2 = off3/Math.atan(tri1); 
     marking2 = off3/Math.atan(tri2); 

希望這樣做的伎倆! :)

+0

好吧,我的印象是,這些數學函數需要浮點型變量..我會給這個去:) – user2815899

相關問題