【问题标题】:Google Cloud Datastore saves a float without decimals as an intGoogle Cloud Datastore 将不带小数的浮点数保存为 int
【发布时间】:2015-02-09 05:46:25
【问题描述】:

我有一个 Google App Engine 应用,在表单中输入了以下内容:

<input class="form-control" name="num" type="number" step="0.01">

然后,在请求处理程序中:

class NumEntity(ndb.Model):
    num = ndb.FloatProperty()


class AddNum(webapp2.RequestHandler):
    def post(self):
        num = self.request.get('num')
        num = float(num.replace(',', '.'))
        new_num = NumEntity(num=num)
        new_num.put()

但是如果我在表单中输入一个不带小数的数字,它就会被存储为一个 int。即使我在逗号后面加上零。

此外,当我对存储为 int 的数字调用 entity.num 时,它会返回 None

这只发生在已部署的应用程序中,当我在本地 SDK 中运行它时,它工作得很好。

【问题讨论】:

    标签: google-app-engine python-2.7 google-cloud-datastore


    【解决方案1】:

    我发现了问题所在。

    我在新开发者控制台的数据存储查看器中手动编辑了实体,与旧控制台不同,它将所有数字视为“数字”,而不是浮点数和整数。因此,当我将不带小数的数字保存为整数时。

    从现在开始,我只使用旧的数据存储查看器。

    【讨论】:

      猜你喜欢
      • 2017-02-23
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2010-12-09
      相关资源
      最近更新 更多