关于数据库:在Doctrine 2中指定十进制字段类型时,缩放和精度是什么意思?

What do scale and precision mean when specifying a decimal field type in Doctrine 2?

我正在为我的symfony2应用程序创建一个十进制字段来保存doctrine2中的财务数字。

目前,情况如下:

1
2
3
4
/**
 * @ORM\Column(type="decimal")
 */
protected $rate;

当我输入一个值并将该值持久化到数据库时,它被四舍五入为整数。我猜我需要为字段设置精度和比例类型,但我需要有人确切地解释它们是做什么的?

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)

但这并没有告诉我太多。

我猜精度是要四舍五入到的小数位数,所以我假设应该是2,但是比例是多少?规模是重要的数字吗?

我的字段声明应该是这个吗?-

1
2
3
4
/**
 * @ORM\Column(type="decimal", precision=2, scale=4)
 */
protected $rate;

条令使用类似于SQL类型的类型。小数恰好是固定精度类型(与浮点不同)。

摘自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.


只是一个简短的提示:我必须从属性精度和比例中删除引号,如下所示:

1
@ORM\Column(type="decimal", precision=8, scale=2)


1
@Column(type="decimal", precision=5, scale=2) means 123.45

1
 * @ORM\Column(type="decimal", precision=10, scale=2)