簡體   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