繁体   English   中英

如何计算 python 和 django 中的百分比

[英]How to calculate percentage in python and django

我正在编写一个简单的程序,以允许用户在十天内获得他们投资的任何金额的 8% 的回报。 十天内的用户余额将等于他/她投资的任何金额的百分之八,十天后该百分比余额将返回到用户余额中。 看看我下面的代码并帮助我。

模型.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) )

我想在这里获取用户投资金额的百分比

视图.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

#获取十天后投资金额的百分比余额

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

这是获得8%金额的方法:

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

这是一个如何调用它的示例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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