I am working with Django and I have a project where I define a Student object that has the fields of grade 1 and grade 2 of type FLOAT, in this class, I build a function that gives back the grade of the student in question although I encountered a problem, the useless digit when I've got an integer number. For example, if your grade is 5 and 9 you get 7.0 and I want to know how can I make the HTML/Django logic syntax display what it should. Just in case it is needed this is the function of how I calculate the grade.
def get_grade(self):
if self.grade_1 is None and self.grade_2 is None:
return "No grades yet"
elif self.grade_2 is None:
return self.grade_1
elif self.grade_1 is None:
return self.grade_2
else:
return (self.grade_1 + self.grade_2)/2
Check if the sum of grade 1 and grade2 will result in float when divided by 2.This can be checked by calculating modulus. If the modulus returns 0, it means result will have 0 in decimal part.
If modulus is 0, return result converted to integer which will remove the decimal part, else return the result as float itself.
Example:
Make changes in the else block as follows:
total = self.grade_1 + self.grade_2
if total%2 == 0:
return (int)(result/2)
else:
return result2