python - 如何在 SQlite python 中限制小数点后的数字?
问题描述
嗨,对不起,如果它看起来如此简单但老实说我在谷歌(或我的实现)上找到的解决方案正在破坏我的代码:/
如何将小数点后的数字限制为 2。即使是 2.99999999,我也想显示为 2.30。
TIA
这是我的这个页面的代码(工作但在小数点后显示很多数字):
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# Update the latest price information for the existing stock
user_id = session["user_id"]
stock_list = db.execute("SELECT stock_id, stock_symbol, shares, unit_price, total_price FROM stocks WHERE owner_name == %s", session["user_id"])
# Iterate through dictionaries in the results (list of rows(dicts))
length = len(stock_list)
for i in range(length):
stock_id = stock_list[i]["stock_id"]
symbol = stock_list[i]["stock_symbol"]
amount = stock_list[i]["shares"]
price_dict = lookup(symbol)
price = price_dict["price"]
total = price * amount
# Update stocks table for the logged in user
db.execute("UPDATE stocks SET unit_price = ?, total_price = ? WHERE stock_id = ?", price, total, stock_id)
# Extract updated data and display in the template
rows = db.execute("SELECT stock_symbol, stock_name, shares, unit_price, total_price FROM stocks WHERE owner_name == %s", session["user_id"])
rows2 = db.execute("SELECT cash FROM users WHERE username == %s", session["user_id"])
assets_list = db.execute("SELECT total_price FROM stocks WHERE owner_name == %s", session["user_id"])
stock_assets = 0.00
cash_asset_list = db.execute("SELECT cash FROM users WHERE username == %s", session["user_id"])
cash_asset = cash_asset_list[0]["cash"]
for i in range(len(assets_list)):
stock_assets = stock_assets + assets_list[i]["total_price"]
net_assets = stock_assets + cash_asset
return render_template("index.html", rows=rows, rows2=rows2, net_assets=net_assets)
我的页面 HTML 模板
{% extends "layout.html" %}
{% block title %}
Home
{% endblock %}
{% block main %}
<table class="table table-hover">
<tr class="table-info">
<th scope="col">Symbol</th>
<th scope="col">Company</th>
<th scope="col">Shares</th>
<th scope="col">Price</th>
<th scope="col">TOTAL</th>
</tr>
{% for row in rows %}
<tr class="table-light">
<td>{{ row.stock_symbol }}</td>
<td>{{ row.stock_name }}</td>
<td>{{ row.shares }}</td>
<td>{{ row.unit_price }}</td>
<td>{{ row.total_price }}</td>
</tr>
{% endfor %}
</table>
<table class="table table-hover">
<tr class="table-dark">
<th scope="col">Cash Balance</th>
<th scope="col">Total Assets</th>
</tr>
{% for row2 in rows2 %}
<tr class="table-light">
<td>{{ row2.cash }}</td>
<td>{{ net_assets }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
解决方案
在 python 中,您可以使用函数round来限制位数的小数。
x = 1.4499999
您可以使用round(x, 小数位数)
x = round(x, 2)
最终输出将是
x = 1.50
推荐阅读
- android - 初始化问题,但已经初始化 Kotlin
- html - 防止 Chrome 中的滚动动画滞后
- python - 如何在python pptx中更改文本框中文本的字体大小
- python - rasterio 正在寻找 gdal 中不再存在的 gcs.csv
- powershell - ADuser 更改变量后未添加 samAccountName
- java - 我的根文件夹不是在 android 10 中创建的吗?
- git - git如何比较同一存储库的两个分支
- python - 为什么我会收到错误:无法在 macOS Catalina 下运行 PySide2 程序的“uic”:“execvp:没有这样的文件或目录”?
- php - 尝试在 Laravel 6 上使用 exclude_if
- java - jar 在 catalina home 的 lib 文件夹中时 context.xml 中的类的 ClassNotFoundException