在第2条中指定decimal字段类型时,scale和precision是什么意思?

时间:2021-10-10 06:49:47

I'm creating a decimal field to hold a financial figure in Doctrine2 for my Symfony2 application.

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

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 documentation says:

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;

4 个解决方案

#1


85  

Doctrine uses types similar to the SQL types. Decimal happens to be a fixed precision type (unlike floats).

Doctrine使用类似于SQL类型的类型。小数恰好是一个固定的精度类型(不像浮点数)。

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)

工资小数(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.

在这个例子中,5是精度,2是刻度。精度表示为值存储的有效数字的数量,而刻度表示可以存储在小数点后的数字的位数。

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.

标准SQL要求十进制(5,2)能够存储任何有5位数和2个小数的值,因此可以存储在工资列范围的值从-999.99到999.99。

#2


23  

Just a quick note: I had to remove the quotes from the attributes precision and scale, like so:

我只是简单地说明一下:我必须从属性的精度和尺度上删除引号,比如:

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

#3


7  

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

#4


0  

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

#1


85  

Doctrine uses types similar to the SQL types. Decimal happens to be a fixed precision type (unlike floats).

Doctrine使用类似于SQL类型的类型。小数恰好是一个固定的精度类型(不像浮点数)。

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)

工资小数(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.

在这个例子中,5是精度,2是刻度。精度表示为值存储的有效数字的数量,而刻度表示可以存储在小数点后的数字的位数。

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.

标准SQL要求十进制(5,2)能够存储任何有5位数和2个小数的值,因此可以存储在工资列范围的值从-999.99到999.99。

#2


23  

Just a quick note: I had to remove the quotes from the attributes precision and scale, like so:

我只是简单地说明一下:我必须从属性的精度和尺度上删除引号,比如:

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

#3


7  

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

#4


0  

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