-4

I am new to android development. I am creating an app in which attendance percentage needs to be calculated. I want to calculate the attendance percentage and place that data in the EDIT TEXT field but when I run the application the data is not calculated instead I have to insert it manually. Please help me out with this. TIA :) Here is my code

float percentage = ((attendClasses / totClasses) * 100);
                        str_percentage = String.valueOf(percentage);
                        ed_percentage.setText(str_percentage);

                        // To insert candidate values into the database
                        boolean result = mySQLiteAdapter.onInsertAttendance(
                                studId, subjId, str_totclasses,
                                str_attendedclasses, str_percentage);
user3530830
  • 7
  • 1
  • 4

2 Answers2

1

Change float percentage = ((attendClasses / totClasses) * 100); to float percentage = ((attendClasses*100) / totClasses);

Here Your attendClasses and totClasses are int value. So division of two int value attendClasses/totClasses will result into 0. As here totClasses>attendClasses.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
  • I tried as u said but the percentage is getting calculated after I click in add percentage and precision values are getting ignored – user3530830 May 04 '15 at 18:27
  • You need to cast result in float like float percentage = (float)(attendClasses*100) / totClasses; for precision values. – Dhaval Patel May 04 '15 at 18:50
-1

try using Float.toString(percentage); instead of String.valueOf(percentage);

Ted
  • 116
  • 10
  • percentage is a primitive type. – Dhaval Patel May 04 '15 at 18:31
  • `float f = Float.parseFloat("25"); String s = Float.toString(25.0f);` check this for reference http://stackoverflow.com/questions/7552660/java-convert-float-to-string-and-string-to-float – Ted May 04 '15 at 18:31