简体   繁体   中英

How to calculate percentage in python and django

Am writing a simple program to allow user to get in return 8 percent of any amount they invest in the period of ten days. The user balance in ten days will equal to Eight percent of any amount he/she invest and the percentage balance will be return into the user balance after ten days. Look at my code below and help me.

Models.py

class Profile(models.Model):
       user = models.ForeignKey(UserModel, on_delete=models.CASCADE)
       balance = models.DecimalField(default=Decimal(0),max_digits=24,decimal_places=4)


class Investment(FieldValidationMixin, models.Model):
       user = models.ForeignKey(User, on_delete=models.CASCADE)
       amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
       fixed_price = models.DecimalField(max_digits=7, decimal_places=2, nulll=True, blank=True)     
       percentageee = models.DecimalField(max_digits=3, decimal_places=0, nulll=True, blank=True)
       profile = models.ForeignKey(Profile, on_delete=models.CASCADE)

       def __str__(self):
              return _("{0}: (#{1} 
              {2}%, ${3})".format(self.__class__.__name__, self.amount, self.percentage, self.fixed_price) )

I want to fetch the percentage of the user investment amount here

Views.py

 def get_percentage(self):
    pef_of_amount = Investment.objects.filter(amount=self.amount).annotate(percentageee=Sum('amount'))
    return '{:%}'.format(amount.percentageee / 8)



def percentile(self, days=10): 
       percent=Investment.objects.filter(self.amount*self.percentage)/100 in days
       return percentile

#get the percentage balance of the amount invested after ten days

def get_current_balance(self,percentile):
   total_expenses = Profile.objects.all().aggregate(Sum('balance')).annonate(percentile)
   return get_current_balance

Here is a method to get 8% of amount:

class Investment(FieldValidationMixin, models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
    fixed_price = models.DecimalField(max_digits=7, decimal_places=2, nulll=True, blank=True)     
    percentageee = models.DecimalField(max_digits=3, decimal_places=0, nulll=True, blank=True)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
       
    def get_percentage(self):
        return self.amount * 0.08

Here is an example of how to call it:

i = Investment()
i.amount = 100
result = i.get_percentage()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM