0

Screen_shot

Hi, sorry if it seems such simple but honestly the solution I find on google (or my implementation) is breaking my code :/

How can i limit the digits as 2 after decimal point. Even it's 2.99999999 I would like to display as 2.30.

TIA

Here my code for this page (working but displays lots of digits after decimal point):

@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)

My HTML template for the page

{% 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 %}
cetins
  • 25
  • 9
  • 1
    Does this answer your question? [How to format a float number in Python Flask and Jinja?](https://stackoverflow.com/questions/46144767/how-to-format-a-float-number-in-python-flask-and-jinja) – zvone Dec 15 '20 at 17:48
  • BTW, this is not about sqlite - it is about formatting in the jinja template – zvone Dec 15 '20 at 17:49
  • Use `round(x, 2)` – IoaTzimas Dec 15 '20 at 17:56
  • 1
    @zvone thanks actually it worked great. Also I've edited the tags and removed sqlite – cetins Dec 15 '20 at 19:19

2 Answers2

1

You can create a function:

def truncate(f):
return math.floor(f * 10 ** 2) / 10 ** 2

And it will return the value without rounding with 2 decimal places.

Then when you do total just set it to the function of the result.

1

In python you can use the function round to limit the decimal for number of places.

x = 1.4499999

you can use round(x, number of decimal places)

x = round(x, 2)

Final output will be

x = 1.50