将float结果转换为double类型

时间:2013-09-29 20:32:12

标签: android parsing math 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.结果计算:

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

1 个答案:

答案 0 :(得分:1)

使用(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);

希望这就是诀窍! :)