【问题标题】:Rounding a pyodbc variables舍入 pyodbc 变量
【发布时间】:2021-09-02 10:04:27
【问题描述】:

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

代码: 视图.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 %}

输出:

总计 485940.85000000003

如何将此值四舍五入到小数点后 2 位(例如 485940.00)?

【问题讨论】:

    标签: python html django rounding pyodbc


    【解决方案1】:

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

    模板.html

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

    查看Jinja's document on topic

    SQL 循环函数也可以完成这项工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多