database 在 Doctrine 2 中指定十进制字段类型时,比例和精度是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14940574/
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
What do scale and precision mean when specifying a decimal field type in Doctrine 2?
提问by Anonymous
I'm creating a decimal field to hold a financial figure in Doctrine2 for my Symfony2 application.
我正在为我的 Symfony2 应用程序创建一个十进制字段来保存 Doctrine2 中的财务数字。
Currently, it looks like this:
目前,它看起来像这样:
/**
 * @ORM\Column(type="decimal")
 */
protected $rate;
When I entered a value and said value was persisted to the database, it was rounded to an integer. I'm guessing that I need to set the precision and scale types for the field, but I need someone to explain exactly what they do?
当我输入一个值并且该值被持久化到数据库时,它被四舍五入为一个整数。我猜我需要为该字段设置精度和比例类型,但我需要有人确切地解释它们的作用?
The Doctrine2 documentationsays:
该Doctrine2文档说:
precision: The precision for a decimal (exact numeric) column (Applies only for decimal column)
scale: The scale for a decimal (exact numeric) column (Applies only for decimal column)
精度:小数(精确数字)列的精度(仅适用于小数列)
scale:小数(精确数字)列的小数位数(仅适用于小数列)
But that doesn't tell me an awful lot.
但这并没有告诉我很多。
I'm guessing precision is the number of decimal places to round to, so I assume that should be 2, but what is scale? Is scale the significant figures?
我猜精度是要四舍五入的小数位数,所以我假设应该是 2,但什么是比例?比例是有效数字吗?
Should my field declaration be this? :-
我的字段声明应该是这个吗?:-
/**
 * @ORM\Column(type="decimal", precision=2, scale=4)
 */
protected $rate;
回答by Louis-Philippe Huberdeau
Doctrine uses types similar to the SQL types. Decimal happens to be a fixed precision type (unlike floats).
Doctrine 使用类似于 SQL 类型的类型。Decimal 恰好是固定精度类型(与浮点数不同)。
Taken from the MySQL documentation:
取自MySQL 文档:
In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:
salary DECIMAL(5,2)
In this example, 5 is the precision and 2 is the scale. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.
Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.
在 DECIMAL 列声明中,可以(并且通常是)指定精度和小数位数;例如:
工资 DECIMAL(5,2)
在本例中,5 是精度,2 是比例。精度表示为值存储的有效位数,小数位数表示可以存储在小数点后的位数。
标准 SQL 要求 DECIMAL(5,2) 能够存储任何五位数和两位小数的值,因此可以存储在薪水列中的值范围从 -999.99 到 999.99。
回答by giggsy
Just a quick note: I had to remove the quotes from the attributes precision and scale, like so:
只是一个简短的说明:我必须从属性 precision 和 scale 中删除引号,如下所示:
@ORM\Column(type="decimal", precision=8, scale=2)
回答by somspeaks
@Column(type="decimal", precision=5, scale=2) means 123.45
回答by Waldemar
I know this one is old, but why oh why are programmers still using decimal... just use 64bit integer and define the value as cents (or thousands) and not wholes. Everything simplifies from that.
我知道这个是旧的,但是为什么程序员还在使用十进制......只需使用 64 位整数并将值定义为美分(或千)而不是整数。一切都由此而简化。
i.e. instead of storing 123.45 just store 12345, do all calculations on integers and when needed present the value to user as 123.45
即不是存储 123.45 而是存储 12345,而是对整数进行所有计算,并在需要时将值作为 123.45 呈现给用户
P.S. And at least for now no-one has 2305843009213693952 cents or 23.058.430.092.136.939,52 wholes on their account.
PS 至少现在没有人在他们的账户上有 2305843009213693952 美分或 23.058.430.092.136.939,52 整数。
回答by Fahim
 * @ORM\Column(type="decimal", precision=10, scale=2)

