Google Cloud Datastore is storing a float as an int if it doesn't have a decimal, how do I parse it?

huangapple go评论65阅读模式
英文:

Google Cloud Datastore is storing a float as an int if it doesn't have a decimal, how do I parse it?

问题

为了测试,我正在使用开发者控制台,并将一个名为Account的实体输入到数据存储中。

type Account struct {
    Balance float64
    Userid  int
}

如果我输入一个余额为10的金额,并使用我的Go应用程序来“获取”实体,我会得到以下结果:

Balance: 0 Userid: 1

然而,如果我将余额更改为10.1,那么我会得到预期的结果:

Balance: 10.1 Userid: 1

这在某种程度上是可以预料的。然而,如果我存储一个没有小数点后面的浮点数,它会被转换为INT类型。在这种情况下,如果我有一个没有小数点的余额,比如1.00,它将无法加载到我的结构体中(它会丢弃两个00)。

英文:

For testing I am using the developer's console and entering an entity of kind Account into the datastore.

type Account struct {
Balance      float64
Userid       int}

If I input a Balance amount of 10 I get the following when using my Go app to "Get" the entity.

Balance:0 Userid:1

However, if I change Balance to 10.1 then I get the expected result

Balance:10.1 Userid:1

This is somewhat expected. However, if I store a float without anything after the decimal it is turned into type INT. In this scenario if I have a balance without a decimal like 1.00 it won't load into my struct. (It will drop the two 00's)

答案1

得分: 2

看起来这是特定于使用开发者控制台的。在开发者控制台下,关于数字的唯一选项是“是一个数字”,它会自动保存为预测的类型。

如果你在你的 Go 应用中使用 Put 来保存实体,它将以 float64 类型存储,并且在提取时也会作为 float64 类型返回。

英文:

It appears this is specific to using the Developer's Console. The only option under the Developer's Console for a number is "is a number" which automatically saves it with it'd predicted type.

If you save the entity using Put in your Go app, it will store it as type float64 and also pull it out as a float64.

huangapple
  • 本文由 发表于 2015年1月22日 04:04:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/28075872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定