MySQL 教义 2 - 浮点数的小数点后 2 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7296027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:59:23  来源:igfitidea点击:

Doctrine 2 - 2 decimal places on a float?

mysqldoctrine-ormsymfony

提问by Tool

Annotation:

注解:

/**
 * @ORM\Column(type="float", scale="2")
 */
protected $curr_price;

I'm using it with Symfony 2.

我在 Symfony 2 中使用它。

And this field becomes a double in MySQL database instead of float with 2 point precision.

并且该字段在 MySQL 数据库中变为双精度值,而不是具有 2 点精度的浮点数。

What am I doing wrong? I tried deleting the DB, reinserting etc...

我究竟做错了什么?我尝试删除数据库,重新插入等...

回答by gilden

Both precisionand scaleproperties work only with the decimalmapping type (link). I suggest you use the decimaltype.

precisionscale性能仅与工作decimal映射类型(链接)。我建议你使用decimal类型。

As to why it's creating a double field instead of float, I'm not entirely sure. It probably has to do with being compatible with all supported databases. I see no mention of doublemapping type so I assume they use the same type for both.

至于为什么它创建一个双字段而不是浮动,我不完全确定。它可能与与所有支持的数据库兼容有关。我没有提到double映射类型,所以我假设它们对两者使用相同的类型。

回答by juan

in the *.yml

在 *.yml 中

curr_price:
    type: decimal
    precision: 10
    scale: 2

回答by Serhii Polishchuk

/**
 * @ORM\Column(type="float", scale=2)
 */
protected $curr_price;

scale should be an integer, and you are using a string

scale 应该是一个整数,你使用的是一个字符串