【发布时间】:2020-04-18 02:31:01
【问题描述】:
我正在从股票 API 中获取一些数据并获得一些值(作为浮点数存储在数据库中) 如下:
YTD Change 0.379996
daily % change 0.00854
我的看法如下:
def get_stock(request):
empty = True
localStocks = Stock.objects.all()
if len(localStocks) > 0 :
empty = False
return render (request,'get_stock.html',{'empty':empty, 'output':list(localStocks)})
还有我的模板
<section class="section-typography container u-readable">
<table>
<thead>
<tr>
<th>Ticker</th>
<th>Name</th>
<th>Price</th>
<th>Last Time</th>
<th>% Change</th>
<th>52W High</th>
<th>52W Low</th>
<th>YTD Change</th>
</tr>
</thead>
<tbody>
{% if not empty %}
{% for list_item in output %}
<tr>
<td> {{list_item.symbol }}</td>
<td> {{ list_item.companyName }}</td>
<td> {{ list_item.latestPrice }}</td>
<td> {{ list_item.latestTime }}</td>
<td> {{ list_item.changePercent }}</td>
<td> {{ list_item.week52High }}</td>
<td> {{ list_item.week52Low}}</td>
<td> {{ list_item.ytdChange }}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</section>
{% endblock %}
股票年初至今的百分比变化不是 0.379996,而是 37.99%,这就是我想要显示的。同样,每日变化不是 0.00854 而是 0.85% 。如何在视图或模板上操作日期以基本上将其相乘并仅显示浮点数的前 2 位小数?
【问题讨论】:
标签: python django python-3.x