在Doctrine 2中指定小数字段类型时,缩放和精度是什么意思? [英] What do scale and precision mean when specifying a decimal field type in Doctrine 2?

查看:491
本文介绍了在Doctrine 2中指定小数字段类型时,缩放和精度是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

目前,它如下所示:

/**
 * @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?

Doctrine2文档说:


precision:十进制(精确数字)列的精度(仅适用于十进制column)

precision: The precision for a decimal (exact numeric) column (Applies only for decimal column)

scale:十进制(精确数字)列的缩放比例(仅适用于十进制列)

scale: The scale for a decimal (exact numeric) column (Applies only for decimal column)

但是这不会告诉我很多。

But that doesn't tell me an awful lot.

我猜测的精度是舍入的小数位数,所以我认为应该是2,但什么是规模?是比例有效数字吗?

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?

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

Should my field declaration be this? :-

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


推荐答案

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

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

取自 MySQL文档


在DECIMAL列声明,精度和比例可以(通常是)指定;例如:

In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:

工资DECIMAL(5,2)

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.

标准SQL要求DECIMAL(5,2)能够存储具有五位数和两位小数的任何值,因此可以存储在salary列中的值的范围为-999.99到999.99。

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.

这篇关于在Doctrine 2中指定小数字段类型时,缩放和精度是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆