【问题标题】:Update SQL query in flask not working with floating/decimal points更新烧瓶中的 SQL 查询不适用于浮点/小数点
【发布时间】:2020-12-19 03:18:32
【问题描述】:

所以,这里相当新。我正在尝试构建一个模拟“购买股票”行为的网络应用程序。

一切正常,获得价值,使用它,直到我需要更新现金的价值(用户必须花费的钱)。因此,当我购买一只股票时,它会显示正确的价格,但当减去该值时,它会在 SQL 表中四舍五入。

这是更新值的代码部分。我无法让它显示 2 个浮点数。

    @app.route("/")
    @login_required
    def index():
        """Show portfolio of stocks"""
        # Get the user ID
        user_id = session.get('user_id', None)
        # Get the symbol. Query first on stocks SQL
        dictionary = db.execute("SELECT symbol, SUM(shares), company FROM buy_stocks WHERE userid=(:userid) GROUP BY company", userid = user_id)
        # Get the number to iterate on the HTML template
        iterate = len(dictionary)
        symbol = [None] * len(dictionary)

(继续,最后:)

    for i in range(len(dictionary)):
            value = int( 100 * price[i] * shares[i] )
            value_temp = value / 100
            total[i] = value_temp
    # Cash still avaiable for more transactions. Sum the total and subtract to the money the user still has
    cash_query = db.execute("SELECT cash FROM users WHERE id=(:userid)", userid = user_id)
    cash = cash_query[0]["cash"]

【问题讨论】:

    标签: html sql flask web decimal-point


    【解决方案1】:

    我为此找到的解决方案是将值提取到另一个函数,并在那里处理它们,强制它们有 2 个小数位。

    这成功了,但是,仍然试图弄清楚它们如何不能从 main 函数本身工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多