首页 > 解决方案 > 如何在 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 %}

标签: pythonhtml

解决方案


在 python 中,您可以使用函数round来限制位数的小数。

x = 1.4499999

您可以使用round(x, 小数位数)

x = round(x, 2)

最终输出将是

x = 1.50

推荐阅读