I have a Django project and I have a {{account.current_balance}} tag in my HTML. I also included the type of data it is just in case it matters. I understand #1 and #2, but I don't understand why with #3, adding intcomma would add a comma after the hundred place.
Tag without floatformat or intcomma
{{ account.current_balance }}
output:
Current Balance - Type
303.6000000000000 - Decimal
Tag with floatformat:2
{{ account.current_balance|floatformat:2 }}
output:
Current Balance - Type
303.60 - Decimal
Tag with floatformat:2 with intcomma
{{ account.current_balance|floatformat:2|intcomma }}
output:
Current Balance - Type
,303.60 - Decimal
adding models.py and views.py
class BankAccount(models.Model):
beginning_balance = models.DecimalField(max_digits=10, decimal_places=2)
class Transaction(models.Model):
amount = models.DecimalField(max_digits=10, decimal_places=2)
transaction_date = models.DateField()
class UserHome(LoginRequiredMixin, AccountContextMixin, ListView):
model = Transaction
template_name = 'base_app/home.html'
context_object_name = 'transactions'
paginate_by = 20
def get_queryset(self):
user = self.request.user
return self.model.objects.filter(user=user).filter(status="Posted").order_by('-transaction_date')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
total_balance = 0
home_get_accounts = BankAccount.objects.filter(in_report='Yes')
for account in home_get_accounts:
posted_transactions_sum = Transaction.objects.filter(bank_account=account, status='Posted').aggregate(total=models.Sum('amount'))['total'] or 0
current_balance = posted_transactions_sum + account.beginning_balance
account.current_balance = current_balance # Adding current_balance attribute to account instance
total_balance += current_balance
print(type(total_balance))
context['home_get_accounts'] = home_get_accounts #for Beginning Balances
context['total_balance'] = total_balance
context['total_balance_type'] = type(total_balance).__name__
return context
This is a bug in Django having issue #35172. It has been solved for the LTS releases in versions 3.2.25+ and 4.2.11+, for the current major version it has been solved in version 5.0.3 onwards.
The solution to fix this would be to upgrade to a version of Django where this is fixed.