如何计算android中的百分比

时间:2014-11-22 06:25:34

标签: android

您好在此代码中如何计算百分比。在edittext框中显示总数,缺席数,存在数和百分比数。在这个如何显示百分比,但我得到的总和现在和缺席的值,但我不知道如何在此代码中找到百分比

任何人都可以帮助我。

的java

total[j] = (EditText) findViewById(R.id.total);
            total[j].setVisibility(View.GONE);
             Present[j] = (EditText) findViewById(R.id.present);
             Present[j].setVisibility(View.GONE);
             Absent[j] = (EditText) findViewById(R.id.absent);
             Absent[j].setVisibility(View.GONE);
             percentage[j] = (EditText) findViewById(R.id.percentage);
             percentage[j].setVisibility(View.GONE);


             float total1 = Float.valueOf(total[j].getText().toString());
             float present1 = Float.valueOf(Present[j].getText().toString());

             float per = ((present1/total1) * 100);
             St_AttendDetails p = (St_AttendDetails) i.next();

            tr = new TableRow(this);


            total[j] = new EditText(this);
            total[j].setKeyListener(null);
            total[j].setText(p.getTotal());
            total[j].setId(p.getId());
            total[j].setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            total[j].setPadding(5, 5, 5, 5);

            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);

            Ll.addView(total[j], params);
            tr.addView((View) Ll);

            Present[j] = new EditText(this);

            Present[j].setKeyListener(null);
            Present[j].setText(p.getPresent());
            Present[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            Present[j].setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(Present[j], params);
            tr.addView((View) Ll);

            Absent[j] = new EditText(this);
            Absent[j].setKeyListener(null);
            Absent[j].setText(p.getAbsent());
            Absent[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            Absent[j].setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(Absent[j], params);
            tr.addView((View) Ll);

            percentage[j] = new EditText(this);
            percentage[j].setKeyListener(null);
            //percentage[j].setText(per);
            percentage[j].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            percentage[j].setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(percentage[j], params);
            tr.addView((View) Ll);
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

logcat的

11-22 08:28:40.259: E/AndroidRuntime(4969): FATAL EXCEPTION: main
11-22 08:28:40.259: E/AndroidRuntime(4969): java.lang.NumberFormatException: Invalid float: ""
11-22 08:28:40.259: E/AndroidRuntime(4969):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at java.lang.StringToReal.parseFloat(StringToReal.java:289)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at java.lang.Float.parseFloat(Float.java:300)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at java.lang.Float.valueOf(Float.java:337)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at com.rushda.attendance.Student_Attendance.addData(Student_Attendance.java:252)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at com.rushda.attendance.Student_Attendance$3$1.run(Student_Attendance.java:128)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at android.os.Handler.handleCallback(Handler.java:725)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at android.os.Looper.loop(Looper.java:137)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at java.lang.reflect.Method.invokeNative(Native Method)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at java.lang.reflect.Method.invoke(Method.java:511)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-22 08:28:40.259: E/AndroidRuntime(4969):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

尝试这样,

float rCtWt = Float.parseFloat(edtRCtWt.getText().toString());
float mk = Float.parseFloat(edtMk.getText().toString());
String finalRWt = ((rCtWt * mk) / 100) + "";
tvFinalRWt.setText(finalRWt);

希望这可以帮助你...