What does scaling and precision mean when specifying the type of a decimal field in Doctrine 2?

I am creating a decimal field to store a financial measure in Doctrine2 for my Symfony2 application.

Currently it looks like this:

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

When I entered the value and the specified value was stored in the database, it was rounded to the nearest integer. I suppose I need to set the accuracy and scale types for the field, but do I need someone to explain exactly what they are doing?

The Doctrine2 documentation says:

precision: precision for the decimal (exact numeric) column (applies only to the decimal column)

scale

: Scale for decimal (exact numeric) column (applies only to decimal column)

But that doesn’t tell me very much.

I assume accuracy is the number of decimal places to round to, so I assume it should be 2, but what is scale? Is the scale significant numbers?

Should my field declaration ?: -

 /** * @ORM\Column(type="decimal", precision=2, scale=4) */ protected $rate; 
+42
types database orm symfony doctrine2
Feb 18 '13 at 16:13
source share
4 answers

Doctrine uses types similar to SQL types. The decimal type is a fixed type of precision (unlike floats).

Taken from MySQL documentation :

In a DECIMAL column declaration, precision and scale can be (and are usually specified); eg:

DECIMAL salary (5.2)

In this example, 5 is precision, and 2 is scale. Accuracy is the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored after the decimal point.

Standard SQL requires that DECIMAL (5.2) can store any value with five digits and two decimal places, so the values ​​that can be stored in the salary column range from -999.99 to 999.99.

+76
Feb 18 '13 at 16:17
source share

A simple note: I had to remove quotation marks from the accuracy and scale of the attributes, for example:

 @ORM\Column(type="decimal", precision=8, scale=2) 
+16
Mar 10 '14 at 16:15
source share
 @Column(type="decimal", precision=5, scale=2) means 123.45 
+7
Jun 06 '16 at 19:33
source share
  * @ORM\Column(type="decimal", precision=10, scale=2) 
0
Sep 27 '14 at
source share



All Articles