2013-09-29 116 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

相关问题