舍入 pyodbc 变量

Rounding a pyodbc variables

我目前有以下代码,它给出了下面提到的输出

代码: Views.py

creditTotal = ' Select SUM(Credit) FROM [Kyle].[dbo].[PostGL] WHERE Credit <> 0.0'
    cursor = cnxn.cursor();
    cursor.execute(creditTotal);
    xCreditTotal = cursor.fetchone()

    return render(request , 'main/Kyletrb.html' , {"xAlls":xAll_l , 'xCreditTotal':xCreditTotal})

Html.html:

{% for xCreditTotal in xCreditTotal %}
  <td><b>{{ xCreditTotal }}</b></td>
  {% endfor %}

输出:

Total 485940.85000000003

我如何才能将该值四舍五入到小数点后两位(例如 485940.00)?

Django 使用 Jinja 模板,所以你可以使用它的圆形文件管理器。它的工作原理如下:

template.html

{% for xCreditTotal in xCreditTotal %}
  <td><b>{{ xCreditTotal| round(2, 'floor') }}</b></td>
{% endfor %}

查看 Jinja's document on topic

SQL round 函数也可以做这个工作:

creditTotal = ' Select ROUND(SUM(Credit) , 2) FROM [Kyle].[dbo].[PostGL] WHERE Credit <> 0.0'