Like what the title saying, I didn't format the value to float... Here is my index() code:

def index():
# get stock and share information
rows = db.execute("SELECT stock, SUM(shares) AS shares FROM 'transaction' WHERE userid=:user_id GROUP BY stock", user_id=session["user_id"])
# get cash for user
cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])
cash = cash[0]["cash"]
# update stock price and value in record
sum_cash = 0
for row in rows:
    row['price'] = lookup(row['stock'])['price']
    row['stock_name'] = lookup(row['stock'])['name']
    sum_cash = sum_cash + row['price']*row['shares']
return render_template("index.html", transaction = rows, cash_hand = cash, sum_cash = sum_cash+cash)

Review the helpers.py section of the spec to remember there is a function supplied that will format floats.

Last in the file is usd, a short function that simply formats a float as USD (e.g., 1234.56 is formatted as $1,234.56)

  • Yeah, I'm using the usd function now in index.html. Looks like working fine. But still, I'm confusing about if I get the price in two decimal and cash in total is two decimal float, how could the result become a long float.... Jun 14 '17 at 0:30
  • If I had to venture a guess, it's because "FLOAT" is more-or-less an alias (affinity) for "REAL" in sqlite see doc on datatypes. Storage class REAL says "The value is a floating point value, stored as an 8-byte IEEE floating point number." Jun 14 '17 at 2:14

Use the format method. The following will give you 2 decimal places, if that's what you want.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .