S
S
SomeOneElseElse2017-05-15 13:29:04
Django
SomeOneElseElse, 2017-05-15 13:29:04

Django, how to subtract a percentage from an amount?

How to subtract the discount percentage of the product from the amount of the product? here is the PS code (the price is indicated through Decimal)

class Product(models.Model):
  name 			= models.CharField(max_length=40)
  slug				= models.SlugField(unique=True, blank=True)
  price 			= models.DecimalField(max_digits=10, decimal_places=2, default=0)
  discount			= models.IntegerField(default=0)
  stock		        = models.PositiveIntegerField(default=1)
  category		 	= models.ForeignKey(ProductCategory, blank=True, null=True, default=None)
  short_description	= models.TextField(max_length=220, blank=True, default=None)
  description		= models.TextField(max_length=500, blank=True, default=None)

  is_active			= models.BooleanField(default=True)
  publish			= models.DateField(auto_now=False, auto_now_add=True)
  timestamp		= models.DateTimeField(auto_now=False, auto_now_add=True)
  

  def __str__(self):
    return "{0}:|:{1}".format(self.price, self.name)

  class Meta:
    ordering 						= ["-timestamp"]
    verbose_name 					= 'Product'
    verbose_name_plural 			= 'Products'



  def get_absolute_url(self):
    #return reverse("products:product_url", kwargs={"slug": self.slug})
    return "/products/product/%s" %(self.slug)

  def save(self, *args, **kwargs):
    discount			= self.discount
    price 			= self.price
    if discount > 0:

    super(Product, self).save(*args, **kwargs)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SomeOneElseElse, 2017-05-15
@SomeOneElseElse

Easier than simple,
If you have a calculator, then there is no problem: type the sum-subtraction operation in% - press the button with the% sign and the calculator itself will give the result. If you want to do it manually, then: 1. It is necessary to convert the percentage amount into a coefficient (divide the percentage by 100); 2. Multiply the amount by the coefficient, get the amount corresponding to the interest; 3. Subtract the amount of interest from the amount. For example, the amount of 100 rubles. , interest-10%. 1. 10/100=0.1; 2. 100x0.1 \u003d 10 rubles. ; 3. 100-10=90 rub. This is the primary school task.
here is the code

def save(self, *args, **kwargs):
    discount						= self.discount
    price 							= self.price
    if discount > 0:
      recount_of_discount 		= ( discount / 100 )
      print(recount_of_discount)
      multiplay_sum_on_coef		=  float(price) * float(recount_of_discount)
      print(multiplay_sum_on_coef)
      from_sum_minus_percent 		=  float(price) - float(multiplay_sum_on_coef)
      print(float(from_sum_minus_percent))
    super(Product, self).save(*args, **kwargs)

A
Artyom Innokentiev, 2017-05-15
@artinnok

PEP8 is under your pillow, and the code is in the furnace.

S
sim3x, 2017-05-15
@sim3x

In [2]: from decimal import Decimal

In [3]: Decimal(10.11) * Decimal(1 - 20/100)
Out[3]: Decimal('8.087999999999999994226840272')

PS Turn off this leveling, for the sake of St. Torvalds

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question